如何在请求之外的Express中访问会话?

| 我知道我可以用
function(req, res) {
    req.session
}
使用快递。但是,我需要在响应功能之外访问会话。我将如何去做? 我正在使用socket.io传递信息以添加帖子和评论。因此,当我在服务器端收到socket.io消息时,我需要使用会话验证发布信息的人。但是,由于这是通过socket.io完成的,因此没有要求。     
已邀请:
        使用socket.io,我以一种简单的方式完成了此操作。我假设您的应用程序有一个对象,例如MrBojangle,因为我的名字叫Shished:
/**
 * Shished singleton. 
 *
 * @api public
 */
function Shished() {
};


Shished.prototype.getHandshakeValue = function( socket, key, handshake ) {                          
    if( !handshake ) {
        handshake = socket.manager.handshaken[ socket.id ];                                         
    }
    return handshake.shished[ key ];                                                                
};                                                                                                  

Shished.prototype.setHandshakeValue = function( socket, key, value, handshake ) {                   
    if( !handshake ) {
        handshake = socket.manager.handshaken[ socket.id ];                                         
    }
    if( !handshake.shished ) {
        handshake.shished = {};                                                                     
    }
    handshake.shished[ key ] = value;                                                               
};
然后使用您的授权方法,将MongoDB用于会话存储:
io.set(\'authorization\', function(handshake, callback) {
    self.setHandshakeValue( null, \'userId\', null, handshake );
    if (handshake.headers.cookie) {
        var cookie = connect.utils.parseCookie(handshake.headers.cookie);
        self.mongoStore()
        .getStore()
        .get(cookie[\'connect.sid\'], function(err, session) {
            if(!err && session && session.auth && session.auth.loggedIn ) {
                self.setHandshakeValue( null,
                            \'userId\',
                            session.auth.userId,
                            handshake );
            }
        });
    }
然后,在将记录保存到模型中之前,您可以执行以下操作:
model._author = shished.getHandshakeValue( socket, \'userId\' );
    
        我想我有一个不同的答案。 码:
var MongoStore = require(\'connect-mongo\')(session);
var mongoStore = new MongoStore({
    db:settings.db,            //these options values may different
    port:settings.port,
    host:settings.host
})
app.use(session({
    store : mongoStore
    //here may be more options,but store must be mongoStore above defined
}));
那么您应该在req处定义一个会话密钥,就像: 码:
req.session.userEmail;
最终,您可以通过以下方式获得它: 码:
var cookie = require(\"cookie\"); //it may be defined at the top of the file
io.on(\"connection\",function(connection){

 var tS = cookie.parse(connection.handshake.headers.cookie)[\'connect.sid\'];
 var sessionID = tS.split(\".\")[0].split(\":\")[1];
 mongoStore.get(sessionID,function(err,session){
      console.log(session.userEmail);
 });
}
我昨天进行了测试,效果很好。     
        我相信检查
socket.handshake
应该可以帮助您完成会议:
io.sockets.on(\'connection\', function(socket) { 
  console.log(socket.handshake.sessionID);
});
当客户端与您的socket.io服务器建立套接字连接时,客户端将发送WebSocket握手请求。我在上面所做的就是从握手中获取会话ID。     
        假设您的socket.io代码看起来像这样:
io.on(\'connection\',
function(client) {
  console.log(client.request)
});
如上例所示,请求为“ 10”。 编辑: 另外,这可能会有所帮助: https://github.com/aviddiviner/Socket.IO-sessions     

要回复问题请先登录注册