PHP 8.6.0 Beta 3 is available for testing

Swoole\Event::write

(PECL swoole >= 1.9.0)

Swoole\Event::writeWrite data to socket asynchronously

Descrizione

public static function Swoole\Event::write(mixed $fd, mixed $data): bool

Makes data sending asynchronous for stream/sockets resources.

Nota:

If data is continuously written to a socket but the peer cannot read fast enough, the socket buffer will eventually fill up. In this case, the Swoole underlying layer will store the excess data in an in-memory buffer and wait for the writable event to trigger before attempting to write to the socket again. However, if the in-memory buffer also becomes full, Swoole will throw a "pipe buffer overflow, reactor will block" error and switch to blocking mode, pausing further writes until space becomes available.

Nota:

The buffer-full return of false is an atomic operation—it guarantees either complete success (all data written) or total failure (nothing written).

Avviso

Event::write cannot be used with SSL/TLS-encrypted streams or sockets (tunneled connections).

Avviso

Upon successful execution, Event::write will automatically switch the $socket to non-blocking mode.

Elenco dei parametri

fd

File descriptor (stream/socket resource, integer, or object).

data

Data to send (length must not exceed socket buffer size).

Valori restituiti

Restituisce true in caso di successo, false in caso di fallimento.

Esempi

Example #1 Swoole\Event::write() example

<?php
use Swoole\Event;

$fp = stream_socket_client('tcp://127.0.0.1:9501');
$data = str_repeat('A', 1024 * 1024*2);
Event::add($fp, function($fp) {
     echo fread($fp);
});
Event::write($fp, $data);
?>

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top