解决think-swoole如何保持websocket连接不中断
muke
每日精进
muke
2022-05-23 14:51:28
1179
{{single.collect_count}}

在vendor/thinkphp/topthink/think-swoole/src/websocket/socketio/Handler.php的onOpen方法中修改代码:

public function onOpen($fd, Request $request)
{
    $this->eio = $request->param('EIO');

    $payload = json_encode(
        [
            'sid'          => base64_encode(uniqid()),
            'upgrades'     => [],
            'pingInterval' => $this->pingInterval,
            'pingTimeout'  => $this->pingTimeout,
        ]
    );

    $this->push(EnginePacket::open($payload));

    $this->event->trigger("swoole.websocket.Open", $request);

    if ($this->eio < 4) {
        //  注释此处,tcp连接一直存在,会导致占用大量内存并不建议此操作
        //  $this->resetPingTimeout($this->pingInterval + $this->pingTimeout);
        $this->onConnect();
    } else {
        $this->schedulePing();
    }
}
回帖
全部回帖({{commentCount}})
{{item.user.nickname}} {{item.friend_time}}
{{item.content}}
{{item.comment_content_show ? '取消' : '回复'}} 删除
回帖
{{reply.user.nickname}} {{reply.friend_time}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}