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\Server->send(int $fd, string $data, int $serverSocket = -1): bool
The file descriptor of the client to send data to
The data you want to send back to the client
The Unix socket address for when you are using `UnixSocket DGRAM, not required when using just TCP
If success, it returns 'true', otherwise it returns false
.
Send data back to the client.
Because the send operation is atomic, multiple processes that call send
to the same TCP connection, they will not conflict with each other.
The max TCP data size is 2MB. This limit can be changed by configuring the configuration of buffer_output_size.
If the buffer is reached, OpenSwoole will write to a temporary file until the buffer is free enough to use again.
When using a UnixSocket, if the buffer becomes full, a temporary file is also used until the buffer is free again. If the buffer is not available for a long period of time, it can lead to a socket memory buffer overflow, OpenSwoole will then immediately return false
and you can then save the data to disk and send again when the buffer is not full.
When coroutines are enabled and a server worker is sending data , the coroutine will yield when the buffer is full automatically to allow other processes to continue but will continue execution once the buffer is free again, allowing the server to operate more concurrently.
If the result is false
, you can call the method OpenSwoole\Util::getLastErrorCode()
to get the last error code.
Listening using UnixSocket DGRAM and sending data to the peer:
<?php
$server->on("packet", function (OpenSwoole\Server $server, $data, $addr)
{
$server->send($addr['address'], 'SUCCESS', $addr['server_socket']);
});