使用Socket.IO时如何访问会话标识符? [英] How to access session identifier when using Socket.IO?

查看:169
本文介绍了使用Socket.IO时如何访问会话标识符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有聊天,我需要管理独特的连接。我搜索过,但我发现的解决方案似乎都被废弃了。



那么,如何获得一个套接字的会话ID与Socket.IO? >

我正在使用 Node.js Express.js Socket.IO

解决方案

使用Socket。 IO认证设置,并将cookie传递到Express的cookie解析器中间件。一旦cookie被解析,您可以获取客户端的会话ID,并从会话存储中获取关联的会话,无论是内存故事还是其他类型的存储。



我们需要为Socket.IO和Express
var parseCookie = express.cookieParser(secret)使用相同的秘密;
var store = / *你的MemoryStore,RedisStore等* /;

io.set('authorization',function(handshake,callback){
if(handshake.headers.cookie){
// pass a req,res,and next好像是中间件
parseCookie(handshake,null,function(err){
handshake.sessionID = handshake.signedCookies ['connect.sid'];
//或者如果你不t已签署Cookie
handshake.sessionID = handshake.cookies ['connect.sid'];

store.get(handshake.sessionID,function(err,session){
if(err ||!session){
//如果我们无法抓取一个会话,请关闭连接
回调('Session not found。',false);
} else {
//保存会话数据并接受连接
handshake.session = session;
callback(null,true);
}
});
}};
} else {
return callback('No session。',false);
}
callback(null,true);
});

每次客户端尝试连接时,都会运行授权功能。它需要握手标题cookie( handshake.headers.cookies ),并将它们传递给 express.cookieParser()。然后,cookie解析器找到会话ID,然后搜索相关会话的存储。然后,我们将关联的会话存储到 handshake.session 中,所以我们可以这样访问它:

  app.get('/',function(req,res){
req.session.property ='a value';
});

io.sockets.on('connection',function(socket){
var session = socket.handshake.session;
session.property // a value
});


I have a chat, and I need to manage unique connections. I searched around, but the solutions that I have found all appear to be deprecated.

So, how would I get a socket's session ID with Socket.IO?

I'm using Node.js, Express.js and Socket.IO.

解决方案

Use the Socket.IO authentication setting, and pass the cookies to the cookie parser middleware from Express. Once the cookie is parsed, you can get the session ID of the client, and fetch the associated session from the session store, whether it is a memory story or other type of store.

// we need to use the same secret for Socket.IO and Express
var parseCookie = express.cookieParser(secret);
var store = /* your MemoryStore, RedisStore, etc */;

io.set('authorization', function(handshake, callback) {
  if (handshake.headers.cookie) {
    // pass a req, res, and next as if it were middleware
    parseCookie(handshake, null, function(err) {
      handshake.sessionID = handshake.signedCookies['connect.sid'];
      // or if you don't have signed cookies
      handshake.sessionID = handshake.cookies['connect.sid'];

      store.get(handshake.sessionID, function (err, session) {
        if (err || !session) {
          // if we cannot grab a session, turn down the connection
          callback('Session not found.', false);
        } else {
          // save the session data and accept the connection
          handshake.session = session;
          callback(null, true);
        }
      });
    });
  } else {
    return callback('No session.', false);
  }
  callback(null, true);
});

Each time a client attempts to connect, the authorization function is run. It takes the handshake header cookies (handshake.headers.cookies), and passes them to express.cookieParser(). The cookie parser then finds the session ID, and then searches the store for the associated session. Then, we store the associated session into handshake.session, so we can access it like this:

app.get('/', function(req, res) {
  req.session.property = 'a value';
});

io.sockets.on('connection', function(socket) {
  var session = socket.handshake.session;
  session.property // a value
});

这篇关于使用Socket.IO时如何访问会话标识符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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