我正在尝试将会话数据保存在handshake
io.set('authorization')
对象中
var io = sio.listen(server);
io.set('authorization', function (handshake, callback) {
if(handshake.headers.cookie) {
var cookies = cookie.parse(handshake.headers.cookie);
var sid = cookieParser.signedCookie(cookies['xygaming'], secrets.sessionSecret);
sessionStore.load(sid, function(err, session) {
if(err || !session) {
return callback('Error retrieving session!', false);
}
// this is not storing the data into the handshake object
handshake.balloons = {
user: session.passport.user,
room: /\/(?:([^\/]+?))\/?$/g.exec(handshake.headers.referer)[1]
};
return callback(null, true);
});
} else {
return callback('No cookie transmitted.', false);
}
});
我有pub sub
的适配器io.adapter(redisIo({
host: 'localhost',
port: 6379,
pubClient: pub, // just redis.createClient()
subClient: sub // just redis.createClient()
}));
然后我想访问io.sockets.on('connection')
内的握手数据,但它不在那里?知道为什么不通过吗?在原始的回购中,它适用于快递3x,但是由于我升级到4x并对我自己进行了一些修改,它没有通过?
io.sockets.on('connection', function (socket) {
console.log(socket.handshake);
// I want to pass the handshake data here, but its undefined??
var hs = socket.handshake
, nickname = hs.balloons.user.username
, provider = hs.balloons.user.provider
, userKey = provider + ":" + nickname
, room_id = hs.balloons.room
, now = new Date()
// Chat Log handler
, chatlogFileName = './chats/' + room_id + (now.getFullYear()) + (now.getMonth() + 1) + (now.getDate()) + ".txt"
// , chatlogWriteStream = fs.createWriteStream(chatlogFileName, {'flags': 'a'});
socket.join(room_id);
});