SIP.js icon indicating copy to clipboard operation
SIP.js copied to clipboard

Connection timeout on websocket

Open matthieusieben opened this issue 11 years ago • 4 comments

For some reason that I cannot determine (I am using a ws proxy), the WebSocket connection sometimes takes a lot of time to fail (probably based on the browser's "connecting timeout").

When this happens (ua.transport.ws.readyState stuck in WebSocket.CONNECTING), I can manually call ua.transport.ws.close() which will trigger a disconnected (code: 1006) connection error and, in turn, the onError handler which will retry to connect.

It would be nice if I could set a wsServerConnectingTimeout setting that would trigger a new attempt at connecting the WS with a lower delay that the browser's default timeout.

matthieusieben avatar Aug 27 '15 12:08 matthieusieben

My current workaround, in case someone else needs this:

var wsServerConnectingTimeout = 5;

ua.on('connecting', handleConnecting);
ua.on('connected', handleConnected);

function handleConnecting () {
  var ua = this;

  if (wsServerConnectingTimeout > 0) {
    clearTimeout(ua.transportConnectingTimer);
    ua.transportConnectingTimer = setTimeout(function () {
      if (ua.transport.ws.readyState !== WebSocket.OPEN) {
        ua.logger.warn('WebSocket connection timeout.');
        ua.transport.ws.close();
      }
    }, wsServerConnectingTimeout * 1000);
  }
}

function handleConnected () {
  clearTimeout(this.transportConnectingTimer);
}

matthieusieben avatar Aug 31 '15 10:08 matthieusieben

These are probably good ideas. I know that the WebSocket connection is a particularly messy area of the code right now, especially with regards to recovering from dropped connections. It could probably use a giant overhaul.

I'll mark this as a feature request for the time being, and we'll loop back around to it when the time comes.

wakamoleguy avatar Oct 16 '15 18:10 wakamoleguy

Yes, nice feature, I need it too.

Demitrius avatar Dec 07 '15 14:12 Demitrius

This feature has been added in 0.11.0 as part of a major Transport refactor. There is a new connectionTimeout configuration option on the new Transport and can be set like so:

new SIP.UA({
  uri: '[email protected]',
  transportOptions: {
       wsServers: [wss://my.favorite.server],
       connectionTimeout: 5
  }
});

If the implementation isn't quite how you imagined, we're always open to PRs- but I'm going to close this for now.

JimGreenberg avatar Jun 25 '18 15:06 JimGreenberg