【问题标题】:express-socket.io-session session variables are not availableexpress-socket.io-session 会话变量不可用
【发布时间】:2015-08-03 09:55:12
【问题描述】:

我正在使用 express-socket.io-session 模块https://github.com/oskosk/express-socket.io-session

我可以在 socket.handshake 上看到会话数据

{
  headers: {
    host: 'xxxxxxx:8000',
    connection: 'Upgrade',
    pragma: 'no-cache',
    'cache-control': 'no-cache',
    upgrade: 'websocket',
    origin: 'http://localhost:8000',
    'sec-websocket-version': '13',
    'user-agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTM L, like Gecko) Chrome/43.0.2357.65 Safari/537.36',
    'accept-encoding': 'gzip, deflate, sdch',
    'accept-language': 'en-US,en;q=0.8,hi;q=0.6,id;q=0.4,ms;q=0.2'
  },
  time: 'Thu May 21 2015 17:06:25 GMT+0530 (India Standard Time)',
  address: '::ffff:xxxxxxx',
  secret: undefined,
  cookies: {

  },
  signedCookies: {

  },
  sessionStore: {
    sessions: {
      'KK--EcvwVZieofM9NwG_Asee9VKPW8fn': '{"cookie":{"originalMaxAge ":null,"expires":null,"httpOnly":true,"path":"/"},"_csrfSecret":"2Tr7DSbOEeXVOw= =","localeCountry":"US","experimentation":{"version":"1.0","treatments":[],"guid ":"764290c314d0a397ab134c29fe9bfc15"},"experimentationTimeStamp":1432208183650}'
    },
    generate: [
      Function
    ],
    _events: {
      disconnect: [
        Function
      ],
      connect: [
        Function
      ]
    }
  },
  sessionID: '6ZS-AMnOsha0FT_ALGrNp_-dp2Ji90yY',
  session: {
    cookie: {
      path: '/',
      _expires: null,
      originalMaxAge: null,
      httpOnly: true
    }
  }
}

socket.handshake.session 没有会话数据。我不清楚如何从 socket.handshake.sessionStore 中提取会话数据

【问题讨论】:

  • 也许我理解错了,但你的所有会话数据不都在 socket.handshake.sessionStore 中吗?会议 KK--EcvwVZieofM9NwG_Asee9VKPW8fn : ........
  • 是的。但正如您所见, sessionStore 有一个对象会话,它将会话数据设置为动态键。并且会话对象本身不是 JSON。 express-socket.io-session 模块显示了通过 socket.handshake.session 访问会话数据的示例。
  • 我假设关键是会话 ID?如果是这样,那么是的,这是动态的,因此您将无法提前知道。

标签: node.js sockets session express


【解决方案1】:

就会话数据而言,它的 JSON 表示为字符串。您可以遍历根对象中的键并解析每个键的值。

var keys = Object.keys(socket.handshake.sessionStore.sessions);
keys.forEach(function(key) {

    // extract string into a json object   
    var sessionObject = JSON.parse(socket.handshake.sessionStore.sessions[key]);

});

【讨论】:

    猜你喜欢
    • 2013-11-25
    • 2016-02-28
    • 1970-01-01
    • 2018-06-30
    • 2018-10-10
    • 1970-01-01
    • 1970-01-01
    • 2017-10-16
    • 2018-03-07
    相关资源
    最近更新 更多