2012-01-19 13 views
8

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); 
    }); 
}); 
+0

+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

Trả lời

4

Từ http://www.danielbaulig.de/socket-ioexpress/

sio.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); 
    }); 

}); 

Edit: auth đang

io.set('authorization', function (handshakeData, callback) { 
    var cookie; 
    // console.log(handshakeData.headers); 
    if (handshakeData.headers && handshakeData.headers.cookie) { 
    cookie = parseCookie(handshakeData.headers.cookie); 
    // where SessionStore is an instance of your mongo store 
    SessionStore.load(cookie['sioapp.sid'], function (err, session) { 
     if (err) { 
     // if we cannot grab a session, turn down the connection 
     console.log(err); 
     } else { 
     // console.log('Successfully decoded the session: ', session); 
     handshakeData.session = session; 
     } 
    }); 
    } 
    callback(null, true); // error first callback style 
}); 

Khi mỗi 60 giây, phiên được chạm (do đó làm mới ed). Khi người dùng ngắt kết nối, phiên bị hủy.

+0

Cả hai chúng tôi đều sử dụng mã từ cùng một bài viết. Tôi đang sử dụng mã ủy quyền tìm thấy thêm lên trang, nhưng vấn đề của tôi là 'sessionID' của tôi (như được sử dụng trong' hs.sessionID') là không chính xác hoặc mối quan hệ giữa phiên socket và cửa hàng phiên Express chỉ đơn giản là không hoạt động như mong đợi. –

+0

Dán toàn bộ mã của bạn, có thể bạn có lỗi đánh máy. – alessioalex

+0

Tôi đã cập nhật với tất cả mã của mình. –

0

Tôi không chắc chắn 60 * 1000 có nghĩa là 60 mn. Tôi sẽ nói nó là 1 triệu.

+0

Đó là mili giây. 1000 ms * 60 là 60 giây. –

Các vấn đề liên quan