I have an Android client application that needs to be able to retry establishing the connection when it was timed out because of the Authentication event not being emitted or not reaching the server.
A common scenario where the timeout would be necessary to be treated by the client is when the network connection is slow.
My quick and a little bit tested "fix" was to add this socket.emit with the custom message Authentication timeout before calling the disconnect.
if (timeout !== 'none') {
setTimeout(function() {
// If the socket didn't authenticate after connection, disconnect it
if (!socket.auth) {
debug('Disconnecting socket %s', socket.id);
socket.emit('unauthorized', {message: 'Authentication timeout'});
socket.disconnect('unauthorized');
}
}, timeout);
}
This is my solution to avoid having the client constantly check if the socket is not connected because of a possible authentication timeout.
I would like to read your opinions on the "fix" that i have presented or any ideas to improve it.
It would also be nice to know on the server side, when the socket was disconnected because of the timeout. (I'm refering to socket.on('disconnect' ...)
This could easily be achieved by setting a socket.authTimeout = false; next to the socket.auth = false; and then before the previously mentioned socket.disconnect set socket.authTimeout = true;.