express-socket.io-session会话变量不可用

时间:2015-05-21 15:53:14

标签: node.js sockets session express

我使用的是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

中提取会话数据

1 个答案:

答案 0 :(得分:0)

就会话数据而言,其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]);

});