Socket.io 授权功能不更新会话数据
我正在尝试使用 Socket.IO 的授权功能来获取会话数据。问题是,即使我注销并销毁会话,Socket.IO 仍然保留旧的会话信息,这显然不理想。你知道我在下面的代码中做错了什么吗?
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);
});
});
I'm trying to use Socket.IO's authorization function to get session data. The problem is that even if I log out and destroy my session, Socket.IO still has the old session information, which is clearly not ideal. Any ideas what I'm doing wrong in the code below?
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);
});
And here is the connection code:
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);
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 http://www.danielbaulig.de/socket-ioexpress/
编辑:授权码
一次每 60 秒,会话就会被触及(从而刷新)。当用户断开连接时,会话将被销毁。
From http://www.danielbaulig.de/socket-ioexpress/
Edit: auth code
Once every 60 seconds, the session is touched (thus refreshed). When the user disconnects, the session is destroyed.
我不确定 60 * 1000 是否意味着 60 mn。我想说是 100 万。
I'm not sure the 60 * 1000 means 60 mn. I would say it is 1 mn.