Websocket connection closed when browser lost internet connection

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Websocket connection closed when browser lost internet connection



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 () ;
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?





Check the documentation of the WebSocket library you are using, if it has an option to change the ping interval or timeout related option to delay the reporting of websocket connection.
– prerak
Aug 16 at 9:13




1 Answer
1



It's happening because web socket connections are designed that way to get disconnected when there's no network. I understand that you do not want your socket to get disconnected and for that you will have to write logic to reconnect once the network connection resumes. So basically you will have to keep track of the data structures associated with your websocket and restore it when a re-connection is attempted.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard