Connection timeout on websocket
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.
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);
}
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.
Yes, nice feature, I need it too.
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.