Join 4,000+ others and never miss out on new tips, tutorials, and more.
Latest version:
pecl install openswoole-22.1.2 | composer require openswoole/core:22.1.5
<?php OpenSwoole\Coroutine\Client->send(string $data): int|bool
The data to send to the connected remote server. Must be a string type and support binary data.
A successful send will return the number of bytes that were sent. A failed send will return false
, you should then use $client->errCode
to find out why.
Send a TCP/UDP package to the connected remote server. Data can be either string or binary.
When a successful send starts to happen, sometimes the $data
may be differing lengths upon the return compared to the actual full $data
length, this is because the socket failed during the data transfer and the socket has been closed resulting in a send which returns how many bytes were managed to be sent, you should then check the error code using $client->errCode
.
<?php
use OpenSwoole\Coroutine\Client;
co::run(function()
{
$client = new Client(OpenSwoole\Constant::SOCK_TCP);
if(!$client->connect('127.0.0.1', 9501, 0.5))
{
echo "Connection failed with error: {$client->errCode}\n";
}
$data = "Hello World! - from OpenSwoole\n";
$result = $client->send($data);
if($result === false || $result !== strlen($data))
{
echo "Failed to correctly send data, error code: $client->errCode\n";
}
echo $client->recv();
$client->close();
});