Iam trying to implement webrtc communication in my project. I have used appr.tc code base for development. The codebase contains two separate websocket implementation for chrome&firefox browser.
if (isChromeApp()) {
this.websocket_ = new RemoteWebSocket(this.wssUrl_, this.wssPostUrl_);
} else {
this.websocket_ = new WebSocket(this.wssUrl_);
}
RemoteWebSocket
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);
}
}
}
}
}
}
};
I don't want to close the WebSocket connection During internet connectivity loss(within 3 minutes), The RemoteWebSocket is working fine with chrome browser. There is no closed event occurred when connectivity loss within 3 minutes. But in firefox, WebSocket connection will be closed immediately.
Is there any way to delay the close event in javascript websocket libray?