javascript - Socket.io 授权功能不更新 session 数据

标签 javascript node.js express socket.io

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

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);
  });
});

最佳答案

来自 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 秒一次, session 被触摸(因此刷新)。当用户断开连接时, session 将被销毁。

关于javascript - Socket.io 授权功能不更新 session 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8928576/

相关文章:

javascript - promise 没有以我期望的方式解决

node.js - 使用 Typescript 表达 4.x

node.js - 为什么我不能在我的 Node 应用程序中重命名这个对象?

node.js - 将文档 ID 作为字段 ID 包含在 firestore 中

express - InversifyJS:每个 HTTP 请求的依赖实例化

javascript - 跨快速路由访问 Firebase Admin

javascript - 使用 setTimeout 在 React 中限制窗口滚动事件

javascript - Puppeteer:转义选择器中的方括号

javascript - 嵌套函数可以具有在范围之外取值的未定义参数吗?

node.js - 在 Express 中使用参数在 url 上提供静态文件