How can I listen to an event destroyed by a session?

I am currently developing an application with Sails.JS.

I want to count the number of online users and update it after logging in / out or expiring the session, but I don’t know how to implement something like an event destroyed by the session and cannot update the number of online users every time the session has expired without logging off the user.

+4
source share
2 answers

As mentioned above, there are no such events in the session implementation by default, the Sails session is close to an ExpressJs session, I recommend reading this article about ExpressJs sessions:

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

store .

, socket.io ( ) , ?

+2

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. :

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

Source: https://habr.com/ru/post/1570021/


All Articles