我试图在Node.js中使用Socket.IO,并试图让服务器为每个Socket.IO客户端提供一个标识.由于套接字代码超出了http服务器代码的范围,因此它无法轻松访问发送的请求信息,因此我假设它需要在连接期间发送.什么是最好的方法
1)通过Socket.IO获取有关谁连接的信息
2)验证他们说自己是谁(我目前正在使用Express,如果这让事情变得更容易)
如何在Socket.IO上添加cookie值?
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.
//...........
}
Run Code Online (Sandbox Code Playgroud)
我已阅读 http://www.danielbaulig.de/socket-ioexpress/ ,但我不需要在node.js上进行会话管理,但只需要在connect.sid中添加一个socket.io客户端ID值.
我想使用 socket.io 在节点服务器中获取 cookie 值
所以每次套接字向服务器发送消息时,我都会检查 cookie 的值
(我不想要会话 cookie)只有在 express 我得到 cookie
router.get('/', function(req, res, next) {
var cookie_id = req.cookies.userIDf;
console.log(cookie_id);
res.render('index', { title: 'Express' });
});
Run Code Online (Sandbox Code Playgroud)
它在这里工作,但使用 socket.io 我无法获得 cookie 任何解决方案?
io.sockets.on('connection', function (socket) {
var cookief =socket.handshake.headers['cookie'];
console.log(cookief); // its give me session id not the cookies
});
Run Code Online (Sandbox Code Playgroud)