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

Latest version: pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5

Declaration

<?php OpenSwoole\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 OpenSwoole\Coroutine\Client;

co::run(function()
{
    $client = new Client(OpenSwoole\Constant::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 September 20, 2022