Using socket.io version 1.4.8 , I tried a bunch of things to get this to work, what ultimately worked for me was to set both socket.io.uri and socket.io.opts.query before calling connect() to reconnect
// Original connection
socket = io.connect('https://socket.example.com/?token=' + data.socket_auth_token, {
reconnection: false,
transports: ['websocket', 'polling'],
'sync disconnect on unload': true
});
// Reconnect
socket.io.uri = 'https://socket.example.com/?token=' + data.socket_auth_token;
socket.io.opts.query = 'token=' + data.socket_auth_token;
socket.connect();
And here is more code for context:
<script src="//js/contrib/socket.io.js"></script>
<script>
socket = io.connect('https://socket.example.com/?token=' + data.socket_auth_token {
reconnection: false,
transports: ['websocket', 'polling'],
'sync disconnect on unload': true
});
function reconnectSocket() {
if (typeof socket != 'undefined' && !socket.connected) {
$.ajax({
url: '/api/socket/',
type: 'POST',
data: {
'action': 'generate_token',
},
dataType: 'json'
}).done(function (data) {
socket.io.uri = 'https://socket.example.com/?token=' + data.socket_auth_token;
socket.io.opts.query = 'token=' + data.socket_auth_token;
socket.connect();
});
}
setTimeout(reconnectSocket, 5000);
}
setTimeout(reconnectSocket, 5000);
</script>