如何在NodeJS中{HTTP}升级时将请求会话数据传递给websockets

时间:2018-04-10 21:16:34

标签: node.js http express websocket express-session

我正在尝试使用会话数据进行实时聊天,但我似乎无法将会话数据传递给websockets。我已经尝试了所有可以找到的解决方案,但它们都给了我错误。 HTTP请求通过手动升级事件转发,我可以在其中访问请求对象。但是,我无法从那里得到req.session(它返回undefined,即使我添加数据),所以我无法将它传递给WebSockets。这是我的服务器端代码:

var express = require('express');
var app = express();
var server = require('http').Server(app); 
var session = require('express-session');
var mongoose = require('mongoose');
var ejs = require('ejs');
var path = require('path');
var ws = require('ws');
var wss = new ws.Server({noServer:true});
app.set('views', path.join(__dirname, './views'));
app.use(express.static(path.join(__dirname, './public')));
app.set('view engine', 'ejs');
app.use(session({
  saveUninitialized: true,
  resave: true,
  cookie: {httpOnly: true}
}));
app.set('port', process.env.PORT || 3000);
server.listen(3000, function(){
    console.log('listening on 3000')
})
server.on('upgrade', function(req,socket,head){
    var pathname = require('url').parse(req.url).pathname;
    //req.session returns undefined here, so I can't pass it to my WebSocket handler
    if(pathname == '/' || ''){
        wss.handleUpgrade(req, socket, head, function(ws){
            wss.emit('connection', ws)
        })
    }
});
app.get('/', function(req,res){
    res.render('index')
});
var connections = [];
function broadcast(data, users){
    for(let i = 0; i < users.length; i++){
        if(users[i].readyState == ws.OPEN){
            users[i].send(data)
        }
    }
}
wss.on('connection', function(connection){
    var index = connections.push(connection) - 1;
    broadcast('joined!', connections);
    connection.on('message', function(msg){
        broadcast(msg, connections)
    })
    connection.on('close', function(){
        connections.splice(index, 1);
        broadcast('User disconnected', connections)
    })
})

1 个答案:

答案 0 :(得分:0)

这是一个老问题,但为了以防万一有人遇到同样的问题,我在此 expressjs issue 中找到了解决方案。

在上述代码的上下文中,解决方案是首先将 express-session RequestHandler 函数存储在一个变量中:

const sessionParser = session({
  saveUninitialized: true,
  resave: true,
  cookie: {httpOnly: true}
});

app.use(sessionParser);

... 然后像这样在 server.on('upgrade'... 的回调中调用该函数:

server.on('upgrade', function(req, socket, head) {
    sessionParser(req, {}, () => {
        // req.session should be defined at this point
        if (condition) {
            wss.handleUpgrade(req, socket, head, function(ws){
                wss.emit('connection', ws, request);
            });
        }
    });
});