2012-01-19 1 views
8

Socket.IO의 인증 기능을 사용하여 세션 데이터를 가져 오려고합니다. 문제는 내가 로그 아웃하여 내 세션을 파괴하더라도 Socket.IO에는 여전히 이전 세션 정보가 있기 때문에 명확하지 않습니다. 아래의 코드에서 내가 뭘 잘못하고 있는지 아이디어?Socket.io 인증 기능이 세션 데이터를 업데이트하지 않습니다.

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 동일한 문제가 있습니다. 클라이언트의 세션 쿠키가 만료되거나 삭제 되더라도 Socket.IO 연결은 여전히 ​​이전 데이터에 액세스 할 수 있으며 세션이 여전히 활성 상태라고 간주합니다. – dbau

답변

4

편집

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

}); 

http://www.danielbaulig.de/socket-ioexpress/에서 : 여기

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

그리고

가 연결 코드 인증 코드를 한 번 60 초마다

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

, 세션이 터치 (따라서 새로 고침 ed). 사용자가 연결을 끊으면 세션이 파괴됩니다.

+0

우리 모두 같은 기사의 코드를 사용하고 있습니다. 나는 그 페이지에서 발견 된 인증 코드를 사용하고 있지만 나의 문제는'sessionID' ('hs.sessionID'에서 사용 된)가 맞지 않거나 소켓 세션과 Express 세션 저장소 사이의 관계가 간단하다는 것입니다 예상대로 작동하지 않습니다. –

+0

전체 코드를 붙여 넣습니다. 오타가있을 수 있습니다. – alessioalex

+0

모든 코드가 업데이트되었습니다. –

0

60 * 1000은 60Mn을 의미하지 않습니다. 나는 그것이 1 백만이라고 말할 것이다.

+0

밀리 초입니다. 1000ms * 60은 60 초입니다. –