NodeJS和Socket.io会话处理

时间:2016-05-31 10:11:09

标签: ios node.js sockets session express

我正在尝试在socket.io连接中设置一个会话(req.session.username)但是它会设置它但是当我刷新并注销req.session.username时,它会记录未定义。

我在router.get回调中使用了socket.io:

router.get('/', function(req, res, next) {
var io = req.app.get('socketio');
io.sockets.on('connection', function(socket) {
    socket.on('login', function(data) {
       var username = data.username;
       utils.getUser(data.username, function(obj) {
           if(typeof obj != 'undefined') {
                   if(data.password == obj.password) {
                       req.session.username = username;
                       socket.emit('loginSuccess', {message: "You have successfully logged in, whats up " + data.username + "?"});
                   } else {
                      socket.emit('loginError', {message: "Invalid password, please try again."});
                   }
               } else {
                   socket.emit('loginError', {message: data.username + " does not exist, please create an account."});
               }
       });
   });
});

 console.log(req.session.username);
   res.render('./users/login', {
     logged: false
   });
});

0 个答案:

没有答案