0

我正在尝试在我的项目中实现 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 连接在互联网连接中断期间(3 分钟内),RemoteWebSocket 与 chrome 浏览器一起工作正常。在 3 分钟内失去连接时没有发生关闭事件。但是在 Firefox 中,WebSocket 连接会立即关闭。

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

4

1 回答 1

0

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

于 2018-08-14T13:05:47.043 回答