ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
# Process\Pool->write 向对端写入数据,必须在`$ipc_mode`为`SWOOLE_IPC_SOCKET`时才能使用。 ```php function Process\Pool::write(string $data); ``` * `$data`:写入的数据内容。可多次调用`write`,底层会在`onMessage`函数退出后将数据全部写入`socket`中,并`close`连接 * 此方法为内存操作,没有`IO`消耗 * 发送数据操作是同步阻塞`IO` 实例 ---- ```php $pool = new Swoole\Process\Pool(2, SWOOLE_IPC_SOCKET); $pool->on("Message", function ($pool, $message) { echo "Message: {$message}\n"; $pool->write("hello "); $pool->write("world "); $pool->write("\n"); }); $pool->listen('127.0.0.1', 8089); $pool->start(); ``` 调用端 ---- ```php $fp = stream_socket_client("tcp://127.0.0.1:8089", $errno, $errstr) or die("error: $errstr\n"); $msg = json_encode(['data' => 'hello', 'uid' => 1991]); fwrite($fp, pack('N', strlen($msg)) . $msg); sleep(1); //将显示 hello world\n var_dump(fread($fp, 8192)); fclose($fp); ```