Websocket从请求标头获取用户配置文件

时间:2015-11-12 09:20:20

标签: node.js http websocket passport.js

对于正常的http请求,我可以配置passport.js,以便我的http服务器可以读取请求标头并反序列化正确的用户配置文件。

app.route('/login').post(function(req, res, next){
    passport.authenticate('local', {}, function(err, user){
        if (err) return res.json(500, {error: err.toString()});
        return res.send({user: user});
    })(req, res, next)
});

但是对于websocket连接,我可以在请求标头中看到会话ID,但是如何将其与passport结合以获取正确的用户配置文件? 我使用request模块来调用相应的API路由,但我觉得我正在绕道而行......

var server = http.createServer();
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({ server: server })

wss.on('connection', function connection(ws) {

  // connected and can see the request header here...

  var cookie = ws.upgradeReq.headers.cookie;
  /* cookie is something like this.
  cookie = {
    aaa: aaa
    bbb: bbb
    'connect.sid': 's%3A4Cxxxx'
  } */

  ws.on('message', function incoming(message) {
    ws.send('your message is: ' + message);
  });

});

0 个答案:

没有答案