From 448741e8e181b7a369b14974c1ba4faa03d60260 Mon Sep 17 00:00:00 2001 From: Michael McDonald Date: Fri, 15 Apr 2016 17:29:57 +0800 Subject: [PATCH] Update code since socket.io-redis now makes socket.join work async, fix sockets since it's now a hash instead of array --- app.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/app.js b/app.js index b4d468d..6a70533 100644 --- a/app.js +++ b/app.js @@ -80,8 +80,8 @@ app.post("/send", function (req, res) { rooms.forEach(function(room) { if (!site.userRoomEnabled) { - nsp.to(room).sockets.forEach(function(socket) { - logger.info({"category":"message sent","site":nsp.name,"requestId":reqId,"room": room, "socketId": socket.id, "event":req.body.event}); + Object.values(nsp.to(room).sockets).forEach(function(socketId) { + logger.info({"category":"message sent","site":nsp.name,"requestId":reqId,"room": room, "socketId": socketId, "event":req.body.event}); }); nsp.to(room).emit.apply(nsp, args); return; @@ -155,7 +155,7 @@ for (var siteName in config.sites) { }; nsp.getSocket = function(socketId) { - return nsp.sockets.filter(function(s) { return s.id == socketId; })[0]; + return nsp.sockets[socketId]; }; // START: Update User's last connected/disconnected(online/offline) time @@ -198,8 +198,14 @@ for (var siteName in config.sites) { } logger.info({"category":"rooms registered","socketId":socket.id,"rooms":data}); Object.keys(socket.rooms).forEach(function(room) { socket.leave(room); }); - data.forEach(function(room) { socket.join(room); }); - next(); + + if (!data.length) { + return next(); + } + + // proceed once last room has completed joining + data.slice(0,-1).forEach(function(room) { socket.join(room); }); + socket.join(data.slice(-1), next); }); });