Join 4,000+ others and never miss out on new tips, tutorials, and more.
4.x is outdated, please check the latest version 22.x
Latest version:
pecl install openswoole-22.1.2
<?php Swoole\Coroutine\Client->set(array $settings): bool
A key-value array of settings to use
none
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.
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 receivingconnect_timeout
: Connection time out onlywrite_timeout
: Receiving timeout onlyread_timeout
: Sending timeout only<?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();
});