From eac92b00c66ba7e86528d8ba82b7d9de5cbb1f5e Mon Sep 17 00:00:00 2001 From: Julian Lam Date: Tue, 28 Jul 2015 12:16:57 -0400 Subject: [PATCH] better error checking with socket broadcast method --- public/src/modules/chat.js | 2 +- src/socket.io/rooms.js | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/public/src/modules/chat.js b/public/src/modules/chat.js index c47999de0a..54b8fbe93f 100644 --- a/public/src/modules/chat.js +++ b/public/src/modules/chat.js @@ -142,7 +142,7 @@ define('chat', ['components', 'taskbar', 'string', 'sounds', 'forum/chats', 'tra } }); - callback(chats); + callback(null, chats); }); socket.on('event:chats.open', function(data) { diff --git a/src/socket.io/rooms.js b/src/socket.io/rooms.js index c32f9a5dc9..615d650cd6 100644 --- a/src/socket.io/rooms.js +++ b/src/socket.io/rooms.js @@ -39,19 +39,29 @@ rooms.broadcast = function(socket, room, msg, data, callback) { callback = callback || function() {}; + // Filter out socketIds that aren't actually connected + socketIds = socketIds.filter(function(id) { + return io.server.sockets.connected.hasOwnProperty(id); + }); + async.map(socketIds, function(id, next) { - var timeout; + var timeout, + timeoutPassed = false; + if (socket.id === id) { return setImmediate(next, null, []); } timeout = setTimeout(function() { + timeoutPassed = true; next(null, []); }, 500); - io.server.sockets.connected[id].emit(msg, data || {}, function(chats) { + io.server.sockets.connected[id].emit(msg, data || {}, function(err, returnData) { clearTimeout(timeout); - next(null, chats); + if (!timeoutPassed) { + next(null, returnData); + } }); }, callback); };