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\Server->sendwait(int $fd, string $data): bool
The file descriptor of the client to send data to
The data to send to the client
If success, it returns 'true', otherwise it returns false
.
Send data to the remote socket in a synchronously blocking way.
There may be some situations where a server needs to send data to the client continuously and needs to wait for that operation to finish before continuing on. When using the normal $server->send
method, it is purely a asynchronous operation and data is stored in memory when sending, using a buffer to queue data, which can lead to a buffer overflow.
By using $server->sendwait
it can solve the buffer overflow problem, sendwait
will wait for the connection to be completely finished before moving on, it will not return until all the data has been sent.
Only available in
SWOOLE_BASE
mode. Only a blocking server should use this function, do not use this with coroutines.
<?php
$server->on('receive', function(Swoole\Server $server, $fd, $reactorId, $data)
{
// Send data back to the client
$server->sendwait($fd, data);
});