Process\Pool->write

最后更新于:2022-04-02 06:36:04

# Process\\Pool->write [TOC] 向对端写入数据,必须在`$ipc_mode`为`SWOOLE_IPC_SOCKET`时才能使用。 ~~~ function Process\Pool::write(string $data); ~~~ * `$data`:写入的数据内容。可多次调用`write`,底层会在`onMessage`函数退出后将数据全部写入`socket`中,并`close`连接 * 此方法为内存操作,没有`IO`消耗 * 发送数据操作是同步阻塞`IO` ## 实例 ~~~ $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(); ~~~ ## 调用端 ~~~ $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); ~~~
';