大神帮忙看看下面一段代码有什么问题? socket.handshake.session为什么是undefined
io.set('authorization', function(handshakeData, accept) {
handshakeData.cookie = Cookie.parse(handshakeData.headers.cookie);
var connectSid = handshakeData.cookie['connect.sid'];
connectSid = cookieParser(connectSid, 'technode');
if (connectSid) {
sessionStore.get(connectSid, function(error, session) {
if (error) {
accept(error.message, false);
} else {
handshakeData.session = session;
if (session._userId) {
accept(null, true);
} else {
accept('No login');
}
}
})
} else {
accept('No session');
}
})
io.sockets.on('connection', function (socket) {
console.log(socket.handshake.session)
Controllers.online(_userId, function(err, user) {
if (err) {
socket.emit('err', { mesg:err })
}else{
socket.broacast.emit('online', user);
}
})
socket.on('disconnect', function(){
Controllers.offline(_userId, function(err, user) {
if (err) {
socket.emit('err', { mesg: err });
} else {
socket.broadcast.emit('offline' , user)
}
})
});
socket.on('getRoom', function () {
Controllers.getOnlineUsers(function (err, users) {
if (err) {
socket.emit('err', {mg: err});
} else {
socket.emit('roomData', {users: users, messages: messages});
}
})
});
socket.on('createMessage', function (message) {
messages.push(message);
io.sockets.emit('messageAdded', message);
});
});
3 回复
授权函数里面的session是有值的…
socket.handshake.session._userId
能取到这个值吗?
可以用 passport.socketio 这个库