Update query string parameter in socket.io reconnect

I am passing a custom query string containing an identifier to authorize the client on the server when it connects via socket.io.

var env = {id:12345}; var socket = io.connect('https://example.com', {query:'i='+env.id, port:443, reconnect:false, secure:true}); 

When a client disconnects for any reason, it can reconnect simply by calling the following function.

 socket.socket.connect(); 

However, during reconnection, env.id usually contains a different value than during initial reconnection, and I want to have the current value in the query string for every reconnection.

The above reconnection method sends the initial value again, even if env.id been changed to another value.

I tried to set the current parameters when calling the reconnect function, but this does not work.

 socket.socket.connect('https://example.com', {query:'i='+env.id, port:443, reconnect:false, secure:true}) 

Is there an easy way to update the query string with the current value of env.id and then reconnect?

Or do I need to destroy the entire socket object after disconnecting it, and then create a new connection using var socket = io.connect() with the current identifier?

+11
parameters
source share
4 answers

I analyzed the socket object and found a solution, so I answer my question.

The query string is stored inside socket.socket.options.query . You can change it to the desired value just before reconnecting.

 socket.socket.options.query = 'i='+env.id; socket.socket.connect(); 
+10
source share

In the current version of socket.io (at the time of writing 1.3.7) you should use

socket.io.opts.query = 'i='+env.id;

But be careful when using this this error

+8
source share

Using socket.io version 1.4.8, I tried a bunch of things to make it work, which ultimately worked for me, 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 the 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> 
+2
source share

You can use reflection as follows:

  Field field = Manager.class.getDeclaredField("opts"); field.setAccessible(true); Manager.Options options = (Manager.Options) field.get(socket.io()); options.query = getQuery(); 
0
source share

All Articles