Swoole\Coroutine\Client->set(...)

4.x is outdated, please check the latest version 22.x


Latest version: pecl install openswoole-22.1.2

Declaration

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

Parameters

settings

A key-value array of settings to use

Return

none


Description

Update the settings of a TCP/UDP Client before using it.

This client is specifically for TCP/UDP, it shares options with the other two related clients HTTP Client and HTTP2 Client. Please also checkout the overall settings as well, see client configuration.


Additional Settings

These settings are only related to this client.

<?php

$client->set([
    'timeout' => 0.5,
    'connect_timeout' => 1.0,
    'write_timeout' => 10.0,
    'read_timeout' => 0.5,
]);
  • timeout: Total timeout, including all timeouts for connection, sending, and receiving
  • connect_timeout: Connection time out only
  • write_timeout: Receiving timeout only
  • read_timeout: Sending timeout only

Quick Example

<?php

use Swoole\Coroutine\Client;

Co\run(function()
{
    $client = new Client(SWOOLE_SOCK_TCP);

    $client->set(array(
        'timeout' => 0.5,
        'connect_timeout' => 1.0,
        'write_timeout' => 10.0,
        'read_timeout' => 0.5,
    ));

    if(!$client->connect('127.0.0.1', 9501, 0.5))
    {
        echo "Connection failed: {$client->errCode}\n";
    }

    $client->send("Hello World!\n");

    echo $client->recv();
    $client->close();
});
Last updated on August 31, 2022