如何在session expressjs中存储套接字对象

时间:2015-03-15 17:35:57

标签: node.js express socket.io

io.on('connect', function (socket) {
    req.session.socket = socket;    
});

我想将套接字对象存储在用户的会话中。但不能,因为回调中的req对象不会持久化更改。下次我尝试访问它时它不可用。我该怎么做呢?

1 个答案:

答案 0 :(得分:0)

试试这个:

io.use(function(socket, next) {
   yourSessionMiddleware(socket.request, socket.request.res, next);
});

io.on('connect', function (socket) {
    socket.request.session.socket = socket;    
});

yourSessionMiddleware看起来像:

var yourSessionMiddleware = session({
    secret: "super secret word"
});