Tôi đang cố gắng sử dụng chức năng ủy quyền của Socket.IO để nhận dữ liệu phiên. Vấn đề là ngay cả khi tôi đăng xuất và hủy phiên của tôi, Socket.IO vẫn có thông tin phiên cũ, điều này rõ ràng không lý tưởng. Bất kỳ ý tưởng những gì tôi đang làm sai trong mã dưới đây?Chức năng cấp phép Socket.io không cập nhật dữ liệu phiên
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);
});
Và đây là mã kết nối:
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);
});
});
+1 Tôi đang gặp vấn đề tương tự. Ngay cả khi cookie phiên của khách hàng hết hạn hoặc bị xóa, kết nối Socket.IO vẫn có quyền truy cập vào dữ liệu cũ và tin rằng phiên đó vẫn hoạt động. – dbau