Javascript 浏览器失去internet连接时,Websocket连接关闭

Javascript 浏览器失去internet连接时,Websocket连接关闭,javascript,websocket,webrtc,apprtc,Javascript,Websocket,Webrtc,Apprtc,我试图在我的项目中实现webrtc通信。我使用appr.tc代码库进行开发。代码库包含两个单独的用于chrome和firefox浏览器的websocket实现 if (isChromeApp()) { this.websocket_ = new RemoteWebSocket(this.wssUrl_, this.wssPostUrl_); } else { this.websocket_ = new WebSocket(thi

我试图在我的项目中实现webrtc通信。我使用appr.tc代码库进行开发。代码库包含两个单独的用于chrome和firefox浏览器的websocket实现

  if (isChromeApp()) {
            this.websocket_ = new RemoteWebSocket(this.wssUrl_, this.wssPostUrl_);
        } else {
            this.websocket_ = new WebSocket(this.wssUrl_);
        }
远程WebSocket

var RemoteWebSocket = function (wssUrl, wssPostUrl) {
    this.wssUrl_ = wssUrl;
    apprtc.windowPort.addMessageListener(this.handleMessage_.bind(this));
    this.sendMessage_({ action: Constants.WS_ACTION, wsAction: Constants.WS_CREATE_ACTION, wssUrl: wssUrl, wssPostUrl: wssPostUrl });
    this.readyState = WebSocket.CONNECTING;
};
RemoteWebSocket.prototype.sendMessage_ = function (message) {
    apprtc.windowPort.sendMessage(message);
};
RemoteWebSocket.prototype.send = function (data) {
    if (this.readyState !== WebSocket.OPEN) {
        throw "Web socket is not in OPEN state: " + this.readyState;
    }
    this.sendMessage_({ action: Constants.WS_ACTION, wsAction: Constants.WS_SEND_ACTION, data: data });
};
RemoteWebSocket.prototype.close = function () {
    if (this.readyState === WebSocket.CLOSING || this.readyState === WebSocket.CLOSED) {
        return;
    }
    this.readyState = WebSocket.CLOSING;
    this.sendMessage_({ action: Constants.WS_ACTION, wsAction: Constants.WS_CLOSE_ACTION });
};
RemoteWebSocket.prototype.handleMessage_ = function (message) {
    if (message.action === Constants.WS_ACTION && message.wsAction === Constants.EVENT_ACTION) {
        if (message.wsEvent === Constants.WS_EVENT_ONOPEN) {
            this.readyState = WebSocket.OPEN;
            if (this.onopen) {
                this.onopen();
            }
        } else {
            if (message.wsEvent === Constants.WS_EVENT_ONCLOSE) {
                this.readyState = WebSocket.CLOSED;
                if (this.onclose) {
                    this.onclose(message.data);
                }
            } else {
                if (message.wsEvent === Constants.WS_EVENT_ONERROR) {
                    if (this.onerror) {
                        this.onerror(message.data);
                    }
                } else {
                    if (message.wsEvent === Constants.WS_EVENT_ONMESSAGE) {
                        if (this.onmessage) {
                            this.onmessage(message.data);
                        }
                    } else {
                        if (message.wsEvent === Constants.WS_EVENT_SENDERROR) {
                            if (this.onsenderror) {
                                this.onsenderror(message.data);
                            }
                            console.log("ERROR: web socket send failed: " + message.data);
                        }
                    }
                }
            }
        }
    }
};
我不想在internet连接丢失期间(3分钟内)关闭WebSocket连接,RemoteWebSocket在chrome浏览器中工作正常。连接在3分钟内丢失时,未发生关闭事件。但在firefox中,WebSocket连接将立即关闭


在javascript websocket库中有没有任何方法可以延迟关闭事件

之所以会发生这种情况,是因为web套接字连接被设计成在没有网络的情况下断开连接。我理解,您不希望您的套接字断开连接,因此,一旦网络连接恢复,您必须编写重新连接的逻辑。因此,基本上,您必须跟踪与websocket关联的数据结构,并在尝试重新连接时恢复它

如果您正在使用的WebSocket库具有更改ping间隔的选项或与超时相关的选项以延迟WebSocket连接的报告,请查看该库的文档。