Swoole\Coroutine\Client->set

Declaration

<?php
Swoole\Coroutine\Client->set(array $settings)

Parameters

settings

The array of settings

Return

Description

Update the settings of a TCP/UDP Client.

Default settings

If there is no explict settings updated, the HTTP client uses the following default values:

The following settings are supported by the client to help with the protocol parsing.

<?php

[   
    'timeout' => 0.5,
    'connect_timeout' => 1.0,
    'write_timeout' => 10.0,
    'read_timeout' => 0.5,
    'open_length_check'     => 1,
    'package_length_type'   => 'N',
    'package_length_offset' => 0, 
    'package_body_offset'   => 4, 
    'package_max_length'    => 2000000,
    'bind_address' => current(swoole_get_local_ip()), // since v4.5.3
    'bind_port' => -1, // since v4.5.3
    'lowercase_header' => false // since v4.5.3
]

Other settings

<?php

// Check EOF
$client->set(array(
    'open_eof_check' => true, // if check the EOF
    'package_eof' => "\r\n\r\n", // EOF
    'package_max_length' => 1024 * 1024 * 2,
));

// Check package length
$client->set(array(
    'open_length_check'     => 1,
    'package_length_type'   => 'N',
    'package_length_offset' => 0,       // The offset of package length variable
    'package_body_offset'   => 4,       // The offset of body of the package
    'package_max_length'    => 2000000,  // The max length of the package
));

// Set the socket buffer size
$client->set(array(
    'socket_buffer_size'     => 1024*1024*2, // 2MB buffer size
));

// Turn off the Nagle TCP algorithm
$client->set(array(
    'open_tcp_nodelay'     =>  true,
));

// Setup SSL files
$client->set(array(
    'ssl_cert_file'     =>  $your_ssl_cert_file_path,
    'ssl_key_file'     =>  $your_ssl_key_file_path,
    'ssl_allow_self_signed' => TRUE,
    'ssl_verify_peer' => TRUE,
    'ssl_cafile' => TRUE,
    'ssl_host_name' => $ssl_host_name,
    'verify_peer' => TRUE,
    'verify_peer_name' => TRUE,
));

// Setup local TCP address
$client->set(array(
    'bind_address'     =>  '192.168.1.100',
    'bind_port'     =>  36002,
));

// Setup socks5 proxy
$client->set(array(
    'socks5_host'     =>  '192.168.1.100',
    'socks5_port'     =>  1080,
    'socks5_username' => 'username',
    'socks5_password' => 'password',
));

// Setup ip and port of the client
$client->set(array(
    'bind_address'     =>  '192.168.1.100',
    'bind_port'     =>  -1,
));

Example

<?php
use Swoole\Coroutine\Client;

Co\run(function() {
    $client = new Client(SWOOLE_SOCK_TCP);
    $client->set(array(
        'open_eof_check' => true, // if check the EOF
        'package_eof' => "\r\n\r\n", // EOF
        'package_max_length' => 1024 * 1024 * 2,
    ));
    if (!$client->connect('127.0.0.1', 9501, 0.5))
    {
        exit("connect failed. Error: {$client->errCode}\n");
    }
    $client->send("hello world\n");
    echo $client->recv();
    $client->close();
});