我想在socket.io
的节点服务器中获取cookie值所以每次套接字向服务器发送消息时我都会检查cookie的值
(我不想要会话cookie)仅在快递中我通过
获取cookie router.get('/', function(req, res, next) {
var cookie_id = req.cookies.userIDf;
console.log(cookie_id);
res.render('index', { title: 'Express' });
});
它的工作在这里但是使用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
});
答案 0 :(得分:3)
io.sockets.on('connection', function (socket) {
var cookief =socket.handshake.headers.cookie;
var cookies = cookie.parse(socket.handshake.headers.cookie);
console.log(cookies);
});
cookief将给出结果
io=5J1m_uXsDZvc61gTAAAA; userID=63kfh16tiu7md3ck7djv8856s0; userIDf=50
因此需要通过
解析获取值cookie.parse();
cookies.io或cookies.userID或cookies.userIDf
结果将是
{ io: 'RqIksBUQW4SM4zA4AAAA', userID: '63kfh16tiu7md3ck7djv8856s0', userIDf: '50' }
如果你想要userIDf
cookies.userIDf将为您提供50