2013-05-19 55 views
0

如何保存會話是否正確保存在不同瀏覽器中的node.js express.io(全部爲最新版本)不同。 我該如何解決這種不正當行爲?Node.js - Express.io:在不同的瀏覽器中保存不同的會話

代碼:

app.get('/home', function(req, res) { 
    req.session.variable = 'value'; 
    req.session.save(function() { 
     console.log(req.session); 
    }); 
    res.send('<script src="/socket.io/socket.io.js"></script>\ 
       <script>var socket = io.connect();</script>\ 
       Home content'); 
}); 

app.io.route('disconnect', function(req) { 
    console.log('-------------------------------'); 
    console.log(req.session); 
    req.session.variable = ''; 
    req.session.save(function() { 
     console.log(req.session); 
     console.log('-------------------------------'); 
    }); 
}); 

情況: 我是/home頁面上,我重新加載頁面。 在每個瀏覽器控制檯的除鉻輸出重裝後(?):

{ cookie: 
    { path: '/', 
    _expires: Mon May 19 2014 01:40:59 GMT+0200, 
    originalMaxAge: 31536000000, 
    httpOnly: true }, 
    variable: 'value' } 

// RELOAD: 

------------------------------- 
{ cookie: 
    { originalMaxAge: 31536000000, 
    expires: '2014-05-18T23:40:59.399Z', 
    httpOnly: true, 
    path: '/' }, 
    variable: 'value', 
    touch: [Function], 
    resetMaxAge: [Function], 
    save: [Function], 
    reload: [Function], 
    destroy: [Function], 
    regenerate: [Function] } 
{ cookie: 
    { originalMaxAge: 31536000000, 
    expires: '2014-05-18T23:40:59.399Z', 
    httpOnly: true, 
    path: '/' }, 
    variable: '', 
    touch: [Function], 
    resetMaxAge: [Function], 
    save: [Function], 
    reload: [Function], 
    destroy: [Function], 
    regenerate: [Function] } 
------------------------------- 
{ cookie: 
    { path: '/', 
    _expires: Mon May 19 2014 01:41:03 GMT+0200, 
    originalMaxAge: 31536000000, 
    httpOnly: true }, 
    variable: 'value' } 

在Chrome中:

{ cookie: 
    { path: '/', 
    _expires: Mon May 19 2014 00:43:37 GMT+0200, 
    originalMaxAge: 31536000000, 
    httpOnly: true }, 
    variable: 'value' } 

// RELOAD 

{ cookie: 
    { path: '/', 
    _expires: Mon May 19 2014 00:43:37 GMT+0200, 
    originalMaxAge: 31536000000, 
    httpOnly: true }, 
    variable: 'value' } 
------------------------------- 
{ cookie: 
    { originalMaxAge: 31536000000, 
    expires: '2014-05-18T23:36:58.926Z', 
    httpOnly: true, 
    path: '/' }, 
    variable: 'value', 
    touch: [Function], 
    resetMaxAge: [Function], 
    save: [Function], 
    reload: [Function], 
    destroy: [Function], 
    regenerate: [Function] } 
{ cookie: 
    { originalMaxAge: 31536000000, 
    expires: '2014-05-18T23:36:58.926Z', 
    httpOnly: true, 
    path: '/' }, 
    variable: '', 
    touch: [Function], 
    resetMaxAge: [Function], 
    save: [Function], 
    reload: [Function], 
    destroy: [Function], 
    regenerate: [Function] } 
------------------------------- 

希望這會成爲Chrome瀏覽器的漏洞,而不是express.io? 奇怪的是,Chrome中的日誌順序與其他瀏覽器不同。

我需要的是一個會話變量應該刪除/空,如果用戶導航離開某個頁面(其中設置該變量)。

回答

0

我想我很久以前解決的問題,通過設置該設置同步斷開上卸載在客戶端這樣的:

var socket = io.connect('your-socket-url.com', { 
    'sync disconnect on unload': true 
});