ExpressJS & Websocket & session sharing

后端 未结 5 1373
情书的邮戳
情书的邮戳 2020-12-01 09:30

I\'m trying to make a chat application based on Node.js. I\'d like to force websocket server (ws library) to using ExpressJS session system. Unfortunately, I\'ve got stuck.

5条回答
  •  庸人自扰
    2020-12-01 10:06

    WS v3.0.0 and above, has changed the behaviour so the given answers won't work out of the box for those versions. For current versions, the signature of the connection method is [function(socket, request)] and the socket no longer contains a reference to the request.

    ws.on(
        'connection',
        function (socket, req)
        {
            sessionParser(
                req,
                {},
                function()
                {
                    console.log(req.session);
                }
            );
        }
    );
    

提交回复
热议问题