0

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?

Abdul Manaf
  • 4,933
  • 8
  • 51
  • 95
  • 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 '18 at 09:13

1 Answers1

0

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.

Raghvendra Kumar
  • 1,328
  • 1
  • 10
  • 26