How to access session in express, outside of the req?

后端 未结 4 1346
梦如初夏
梦如初夏 2021-02-04 01:14

I know that I can use

function(req, res) {
    req.session
}

using express. However I need to access the session outside of the response func

4条回答
  •  南笙
    南笙 (楼主)
    2021-02-04 02:01

    I believe checking socket.handshake should get you the session:

    io.sockets.on('connection', function(socket) { 
      console.log(socket.handshake.sessionID);
    });
    

    When the client establishes a socket connection with your socket.io server, the client sends a WebSocket handshake request. What I'm doing above is grabbing the session ID from the handshake.

提交回复
热议问题