使用connect-
redis并将redis用作所有经过身份验证的用户的会话存储。确保通过身份验证将密钥(通常是req.sessionID)发送给客户端。让客户端将此密钥存储在cookie中。
在套接字连接(或以后的任何时间)上,从cookie中获取此密钥并将其发送回服务器。使用此密钥以redis方式获取会话信息。(获取密钥)
例如:
服务器端(使用redis作为会话存储):
req.session.regenerate...res.send({rediskey: req.sessionID});客户端:
//store the key in a cookieSetcookie('rediskey', <%= rediskey %>); //http://msdn.microsoft.com/en-us/library/ms533693(v=vs.85).aspx//then when socket is connected, fetch the rediskey from the document.cookie and send it back to servervar socket = new io.Socket();socket.on('connect', function() { var rediskey = Getcookie('rediskey'); //http://msdn.microsoft.com/en-us/library/ms533693(v=vs.85).aspx socket.send({rediskey: rediskey});});服务器端:
//in io.on('connection')io.on('connection', function(client) { client.on('message', function(message) { if(message.rediskey) { //fetch session info from redis redisclient.get(message.rediskey, function(e, c) { client.user_logged_in = c.username; }); } });});


