websocket链接断开服务器报错java.io.EOFException解决办法

canca3年前 (2021-11-16)JavaServer Page1118

检查代码发现没做心跳。。。。加上心跳异常就解决了。


附上代码:

//心跳检测
var heartCheck = {
    timeout: 60000,//60秒
    timeoutObj: null,
    serverTimeoutObj: null,
    reset: function(){
        clearTimeout(this.timeoutObj);
        clearTimeout(this.serverTimeoutObj);
        return this;
    },
    start: function(){
        var self = this;
        this.timeoutObj = setTimeout(function(){
            //这里发送一个心跳,后端收到后,返回一个心跳消息,
            //onmessage拿到返回的心跳就说明连接正常
            websocket.send("HeartBeat");
            self.serverTimeoutObj = setTimeout(function(){//如果超过一定时间还没重置,说明后端主动断开了
                websocket.close();//如果onclose会执行reconnect,我们执行ws.close()就行了.如果直接执行reconnect 会触发onclose导致重连两次
            }, self.timeout)
        }, this.timeout)
    }
}

websocket.onmessage = function(){
    heartCheck.reset();
}


相关文章

frps + nginx 配置 websocket

nginx 配置如下:server {         listen 80;  &nb...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。