大网数据广告活动
PHP 语言与运行手册

Swoole\Event::write · Write data to socket asynchronously

· 52CE 我爱测

Swoole\Event::write — Write data to socket asynchronously

Swoole\Event::write

(PECL swoole >= 1.9.0)

Swoole\Event::write — Write data to socket asynchronously

说明

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

Makes data sending asynchronous for stream/sockets resources.

注意:

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.

注意:

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

警告

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

警告

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

参数

fd

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

data

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

返回值

成功时返回 true, 或者在失败时返回 false。

示例

示例 #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);
?>

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。