我正在尝试在我的项目中实现 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中的关闭事件?