我怎样才能听会话被破坏的事件?

我目前正在用Sails.JS开发一个应用程序。

我想统计在线用户的数量,并更新它,一旦他们login/注销或有会议过期,但我不知道如何实现像会话销毁事件,不能更新在线用户的数量,每当一个会话过期而没有用户注销。

如上所述,在缺省会话实现中没有这样的事件,Sails会话接近ExpressJs会话,我build议您阅读有关ExpressJs会话的这篇文章:

http://expressjs-book.com/forums/topic/express-js-sessions-a-detailed-tutorial/

然后,一个想法,以实现你想要的可能是使用一个store和查询里面。

你有没有关于其他解决scheme,如使用socket.io (内置的风帆),并添加您的用户login频道,然后简单地计数你的频道内的用户?

你可以像这样包装session.destroy()函数:

 var destroyWrapper = buildDestroyWrapper(function(req){ //do stuff after req.destroy was called }); function buildDestroyWrapper(afterDestroy){ return function(req){ req.destroy(); afterDestroy(req); }; } //later, in your controller function controllerAction(req,res,next){ destroyWrapper(req); } 

此方法允许您以不同方式处理销毁,具体取决于您传递给buildDestroyWrapper的callback。 例如:

 var logAfterDestroy = buildDestroyWrapper(function(req){ console.log("session destroyed"); }); var killAfterDestroy = buildDestroyWrapper(function(req){ process.kill(); }); function buildDestroyWrapper(afterDestroy){ return function(req){ req.destroy(); afterDestroy(req); }; } //later, in your controller function logoutAction(req,res,next){ logAfterDestroy(req); } function killAppAction(req,res,next){ killAfterDestroy(req); }