解决think-swoole如何保持websocket连接不中断
MUKE VIP1
Believe that you can do better
文章
30
评论
35
粉丝
1
MUKE
VIP1
2022-05-23 14:51:28
179

在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();
    }
}
回帖
回帖
全部回帖({{crcount}})
{{item.user.nickname}} {{item.user.group_title}} {{common.diaplayTime(item.create_time)}}
{{item.content}}
{{item.comment_content_show ? '取消' : '评论'}} 删除
回帖
{{reply.user.nickname}} {{reply.user.group_title}} {{common.diaplayTime(reply.create_time)}}
{{reply.content}}
{{reply.comment_content_show ? '取消' : '回复'}} 删除
回帖
收起
没有更多啦~
{{commentLoading ? '加载中...' : '查看更多评论'}}