OpenSwoole Server sendwait

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

Declaration

<?php OpenSwoole\Server->sendwait(int $fd, string $data): bool

Parameters

fd

The file descriptor of the client to send data to

data

The data to send to the client

Return

success

If success, it returns 'true', otherwise it returns false.

Description

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 OpenSwoole\Server::SIMPLE_MODE mode. Only a blocking server should use this function, do not use this with coroutines.

Example

<?php
$server->on('receive', function(OpenSwoole\Server $server, $fd, $reactorId, $data)
{
    // Send data back to the client
    $server->sendwait($fd, data);
});
Last updated on September 20, 2022