【问题标题】:Socket.io authorization function is not updating session dataSocket.io 授权功能不更新会话数据
【发布时间】:2012-02-14 06:15:45
【问题描述】:

我正在尝试使用 Socket.IO 的授权功能来获取会话数据。问题是即使我注销并销毁我的会话,Socket.IO 仍然有旧的会话信息,这显然不理想。任何想法我在下面的代码中做错了什么?

io.set('authorization', function (data, accept) {
    if(data.headers.cookie) {
        data.cookie = parseCookie(data.headers.cookie);
        data.sessionID = data.cookie['express.sid'];
        app.set('mongo-store').get(data.sessionID, function (err, session) {
            console.log(err, session);
      if (err || !session) {
                // if we cannot grab a session, turn down the connection
                accept('Error', false);
      } else {
        // save the session data and accept the connection
        data.session = session;
        accept(null, true);
      }
        });
    } else {
        return accept('No cookie transmitted.', false);
    }
    accept(null, true);
});

这里是连接代码:

io.sockets.on('connection', function(socket) {  

  var hs = socket.handshake;
  console.log('A socket with sessionID ' + hs.sessionID 
      + ' connected!');
  // setup an inteval that will keep our session fresh
  var intervalID = setInterval(function () {
      // reload the session (just in case something changed,
      // we don't want to override anything, but the age)
      // reloading will also ensure we keep an up2date copy
      // of the session with our connection.
      hs.session.reload( function () { 
          // "touch" it (resetting maxAge and lastAccess)
          // and save it back again.
          hs.session.touch().save();
      });
  }, 60 * 1000);
  socket.on('disconnect', function () {
      console.log('A socket with sessionID ' + hs.sessionID 
          + ' disconnected!');
      // clear the socket interval to stop refreshing the session
      clearInterval(intervalID);
  });
});

【问题讨论】:

  • +1 我遇到了同样的问题。即使客户端的会话 cookie 过期或被删除,Socket.IO 连接仍然可以访问旧数据并认为会话仍然处于活动状态。

标签: javascript node.js express socket.io


【解决方案1】:

来自http://www.danielbaulig.de/socket-ioexpress/

sio.sockets.on('connection', function (socket) {
    var hs = socket.handshake;
    console.log('A socket with sessionID ' + hs.sessionID 
        + ' connected!');
    // setup an inteval that will keep our session fresh
    var intervalID = setInterval(function () {
        // reload the session (just in case something changed,
        // we don't want to override anything, but the age)
        // reloading will also ensure we keep an up2date copy
        // of the session with our connection.
        hs.session.reload( function () { 
            // "touch" it (resetting maxAge and lastAccess)
            // and save it back again.
            hs.session.touch().save();
        });
    }, 60 * 1000);
    socket.on('disconnect', function () {
        console.log('A socket with sessionID ' + hs.sessionID 
            + ' disconnected!');
        // clear the socket interval to stop refreshing the session
        clearInterval(intervalID);
    });

});

编辑:授权码

io.set('authorization', function (handshakeData, callback) {
  var cookie;
  // console.log(handshakeData.headers);
  if (handshakeData.headers && handshakeData.headers.cookie) {
    cookie = parseCookie(handshakeData.headers.cookie);
    // where SessionStore is an instance of your mongo store
    SessionStore.load(cookie['sioapp.sid'], function (err, session) {
      if (err) {
        // if we cannot grab a session, turn down the connection
        console.log(err);
      } else {
        // console.log('Successfully decoded the session: ', session);
        handshakeData.session = session;
      }
    });
  }
  callback(null, true); // error first callback style
});

每 60 秒触摸一次会话(从而刷新)。当用户断开连接时,会话被销毁。

【讨论】:

  • 我们都在使用同一篇文章中的代码。我正在使用页面上方的授权代码,但我的问题是我的sessionID(在hs.sessionID 中使用)不正确,或者套接字会话和 Express 会话存储之间的关系根本不起作用正如预期的那样。
  • 我认为我们都缺少的是disconnect 方法中的hs.session.destroy()。你觉得这对吗?
  • 仅当您想在断开连接时完全销毁会话时。我只是想停止“触摸”它。
【解决方案2】:

我不确定 60 * 1000 是否意味着 60 百万。我会说是一百万。

【讨论】:

  • 毫秒。 1000 毫秒 * 60 是 60 秒。
猜你喜欢
  • 2023-03-31
  • 2012-08-24
  • 2020-01-05
  • 2016-05-11
  • 2014-08-14
  • 2011-07-25
  • 2011-09-07
  • 2021-10-08
  • 1970-01-01
相关资源
最近更新 更多