Swoole 一个 Server 监听多个端口, 代码如下
// 前端用的
$ws = new \swoole_websocket_server('0.0.0.0', 9502);
$ws->on('message', function ($server, $frame) {
});
// 服务端用
$tcp_server = $ws->addListener('0.0.0.0', 9501, SWOOLE_SOCK_TCP);
$tcp_server->set([]);
$tcp_server->on('receive', function ($server, $fd, $threadId, $data) {
});
TCP Client 发送 json 数据到 TCP Server
$client = new \swoole_client(SWOOLE_SOCK_TCP);
$address = '127.0.0.1';
$port = '9501';
if ($client->connect($address, $port, -1)) {
try {
$result = $client->send(json_encode(['code' => 'order_receive', 'data' => 39]));
$this->info($result);
$client->close();
} catch (\Exception $exception) {
$this->error($exception->getMessage());
}
}
发生错误:
Swoole\Client::send(): failed to send(5) 34 bytes, Error: Broken pipe[32]
最近才接触 Swoole, 不是很懂, 请大神指点