在Socket.IO上添加cookie值 [英] Adding a cookie value on Socket.IO

查看:616
本文介绍了在Socket.IO上添加cookie值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Socket.IO上添加Cookie值?

How to add a cookie value on 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. 

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

我读了
http://www.danielbaulig.de/socket-ioexpress/
,但我不需要在node.js上的会话管理,但只需要在cookie中添加一个socket.io客户端ID值作为connect.sid。

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

推荐答案

使用Socket.io查看授权。这是处理初始连接请求的地方,您可以在其中设置响应头。

Take a look at Authorization with Socket.io. This is where it handles the initial connection request, and where you can set a response header.

我不确定 cookie ,value)速记工作,但您可以尝试手动设置:

I'm not sure if the cookie(name,value) shorthand works, though you can try setting 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=/";

这篇关于在Socket.IO上添加cookie值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆