node.js - Handle disconnect / logout events -
after connecting client subscribes specific room contains connected users:
io.socket.get('/connection/subscribe', function(data, jwr) { io.socket.on('user_connected', function(user) { console.log('new bro connected'); }); });
at moment i'm storing user ids in memory. therefor use single variable,
var connectionids = []; /*...*/ subscribe: function(req, res) { if(typeof req.user !== 'undefined') { sails.sockets.join(req.socket, 'users_online'); user.find(req.user.id).exec(function(err, user) { if(connectionids.indexof(user[0].id) == -1) { connectionids.push(user[0].id); } sails.sockets.broadcast('users_online', 'user_connected', user); }); return res.ok(); } },
everything works fine far, problem handling disconnect or logout events. after closing window, logging out or similar need unsubsribe users_online
room.
already tried
io.socket.on('disconnect', function() { io.socket.post('/disconnect', function() { }));
but sadly request never sent. how handle disconnect event properly?
may example helps you: http://socket.io/docs/#sending-and-receiving-events
disconnect fired inside of .on('connection')
. can detect disconnecting on server-side , client-side of project.
Comments
Post a Comment