【问题标题】:Websocket connection closed when browser lost internet connection当浏览器失去互联网连接时,Websocket 连接关闭
【发布时间】:2019-01-17 23:58:45
【问题描述】:

我正在尝试在我的项目中实现 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);
                        }
                    }
                }
            }
        }
    }
};

我不想关闭 WebSocket 连接在 Internet 连接中断期间(3 分钟内),RemoteWebSocket 在 Chrome 浏览器中运行良好。在 3 分钟内失去连接时没有发生关闭事件。但是在Firefox中,WebSocket连接会立即关闭。

有没有办法延迟javascript websocket libray中的关闭事件?

【问题讨论】:

  • 查看您正在使用的 WebSocket 库的文档,是否有更改 ping 间隔或超时相关选项以延迟报告 websocket 连接的选项。

标签: javascript websocket webrtc apprtc


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2018-11-26
    • 2012-07-30
    • 2016-07-22
    • 2020-04-06
    • 2015-08-22
    • 1970-01-01
    • 2015-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多