Coroutine TCP/UDP Client

Coroutine TCP/UDP client.

Instead of using the Coroutine Socket API, You can use the Swoole Coroutine Client API.

  • Swoole\Coroutine\Client->__construct
  • Swoole\Coroutine\Client->set
  • Swoole\Coroutine\Client->connect
  • Swoole\Coroutine\Client->send
  • Swoole\Coroutine\Client->recv
  • Swoole\Coroutine\Client->close
  • Swoole\Coroutine\Client->peek

    Example:

    <?php
    $client = new Swoole\Coroutine\Client(SWOOLE_SOCK_TCP);
    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();
    

    Settings:

    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,
    ]
    
    Swoole\Coroutine\Client->set(array $settings): bool

    For example, you can enable the settings:

    <?php
    $client->set(array(
        'timeout' => 0.5,
        'connect_timeout' => 1.0,
        'write_timeout' => 10.0,
        'read_timeout' => 0.5
    ));
    
    Swoole\Coroutine\Client->connect(string $host, int $port, float $timeout = 0.1) : bool

    Connect to the remote server.

    Example:

    <?php
    if ($cli->connect('127.0.0.1', 9501)) {
          $cli->send("data");
    } else {
          echo "connect failed.";
    }
    
    Swoole\Coroutine\Client->send(string $data)

    Send data to the remote server.

    Swoole\Coroutine\Client->recv(float $timeout = -1) : string

    Receive data from the remote server.

    Swoole\Coroutine\Client->close() : bool

    Close the connection.

    Swoole\Coroutine\Client->peek(int $length = 65535) : string

    Check the data in the socket buffer.