Adding cookie value to Socket.IO

How to add cookie value to Socket.IO?

io.sockets.on('connection', function (client)
{ 
    console.log(client.id); 
    client.handshake.headers.cookie.socketID = client.id;   // not work
 // client.handshake.headers.cookie("socketID",client.id);  //crash

    console.log(client.handshake.headers.cookie);
// The current output is:
//connect.sid=tI21xumy3u2n4QXO1GljmPAf.pzFQ1Xu%2B6bz36secu4VSCdSNU8PT1L44gQZ4kFUFQqQ
//this is express session id, and I also want to add client.id of Socket.IO. 

//...........
}

I read http://www.danielbaulig.de/socket-ioexpress/ , but I do not need session management on node.js, but just need to add the socket id value of the client ID to the cookie, as connect.sid does.

+5
source share
2 answers

See Authorization with Socket.io. Here it processes the initial connection request and where you can set the response header.

I'm not sure if the haircut works cookie(name,value), although you can try installing it manually:

    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000)); // set day value to expiry
    var expires = "; expires="+date.toGMTString();

    handshake.headers.cookie = name+"="+value+expires+"; path=/";
+10
source

, cookie , . , socket.io .

socket.io 1.3.4,

\node_modules\socket.io\node_modules\engine.io\lib\server.js

249 :

headers['Set-Cookie'] = self.cookie + '=' + id;

headers['Set-Cookie'] = req.headers.cookie;

-2

All Articles