我使用node.js和socket.io。在客户端,我写了这样一个cookie:如何访问socket.io连接中的cookie数据?
var socket = io.connect('http://localhost:3000');
document.cookie="foo=bar";
socket.emit('this', { is: 'test'});
,并在服务器端,我需要能够从该cookie读取socket.io连接里面,像这样:
io.sockets.on('connection', function (socket) {
socket.on('this', function(reqData){
console.log(socket.handshake.headers); // there is no cookies here!
});
}
无框架解决方案是首选,任何帮助表示赞赏。 谢谢。
更新:在this gist你可以找到我的完整代码。
请参阅此问题:[如何存储Cookie中插座 - io-socket-handshake-headers-cookie](http://stackoverflow.com/questions/19446602/how-to-store- cookie-in-socket-io-socket-handshake-headers-cookie/19451088#19451088) –
@MatthiasHoldorf感谢您的帮助,但我已经看到了。涉及一些第三方模块,并且某些部分的答案已过时。 –