From 59545d740c3a433fa28edf914d30a408bd8c2951 Mon Sep 17 00:00:00 2001 From: barisusakli Date: Tue, 16 Dec 2014 19:40:47 -0500 Subject: [PATCH] only emit event:user_leave to topic rooms instead of all online users --- src/socket.io/index.js | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/socket.io/index.js b/src/socket.io/index.js index 6c19f356a2..2df9e3ead8 100644 --- a/src/socket.io/index.js +++ b/src/socket.io/index.js @@ -49,8 +49,14 @@ function onConnection(socket) { onConnect(socket); - socket.on('disconnect', function() { - onDisconnect(socket); + // see https://github.com/Automattic/socket.io/issues/1814 and + // http://stackoverflow.com/questions/25830415/get-the-list-of-rooms-the-client-is-currently-in-on-disconnect-event + socket.onclose = function(reason) { + Object.getPrototypeOf(this).onclose.call(this, {reason: reason, rooms: socket.rooms.slice()}); + }; + + socket.on('disconnect', function(data) { + onDisconnect(socket, data); }); socket.on('*', function(payload) { @@ -91,16 +97,19 @@ function onConnect(socket) { } } -function onDisconnect(socket) { +function onDisconnect(socket, data) { if (socket.uid) { var socketCount = Sockets.getUserSocketCount(socket.uid); if (socketCount <= 0) { socket.broadcast.emit('event:user_status_change', {uid: socket.uid, status: 'offline'}); } - // TODO: if we can get socket.rooms here this can be made more efficient, - // see https://github.com/Automattic/socket.io/issues/1897 - io.sockets.in('online_users').emit('event:user_leave', socket.uid); + // see https://github.com/Automattic/socket.io/issues/1814 + data.rooms.forEach(function(roomName) { + if (roomName.startsWith('topic')) { + io.sockets.in(roomName).emit('event:user_leave', socket.uid); + } + }); } }