Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update query string parameter at socket.io reconnect

I pass a custom query string containing an ID to authorize a client on the server when he is connecting 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 whatever reason, he can reconnect by simply calling the following function.

socket.socket.connect();

However, at the time of a reconnect env.id usually contains a different value than during the initial connect and I want to have the current value in the query string at each reconnect.

The above reconnect method sends the initial value again, even if env.id was changed to an other value meanwhile.

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

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

Is there a simple way to update just the query string with the current env.id value and then reconnect?

Or do I need to destroy the entire socket object once disconnected and then build up a new connection using var socket = io.connect() with the current id?

like image 573
1nsane Avatar asked Feb 23 '14 14:02

1nsane


People also ask

Does socket IO reconnect automatically?

In the first case, the Socket will automatically try to reconnect, after a given delay.

Does socket IO use long polling?

First, Socket.IO creates a long-polling connection using xhr-polling. Then, once this is established, it upgrades to the best connection method available.


1 Answers

I analyzed the socket object and found the solution, therefore I am answering my own question.

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

socket.socket.options.query = 'i='+env.id;
socket.socket.connect();
like image 69
1nsane Avatar answered Oct 13 '22 14:10

1nsane