Swoole TCP Client Error Broken pipe [32]

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, 不是很懂, 请大神指点

讨论数量: 3
CodingHePing

不要发送了消息,马上close掉连接

5年前 评论

@_Peter 我把 close 方法去掉了, 但还是一样的错误

5年前 评论

@_Peter 大佬,我也是这样,但这种情况无法简单的使用close来解决,而且我现在没法捕捉到服务端断开的消息,业务场景是这样的
由于客户端活跃度不够,服务端主动断开了连接,这个时候,客户端怎么才能拿到服务端断开连接的消息?

5年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!