io.on('connect', function (socket) {
req.session.socket = socket;
});
我想将套接字对象存储在用户的会话中。但不能,因为回调中的req对象不会持久化更改。下次我尝试访问它时它不可用。我该怎么做呢?
答案 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"
});