From 392d9dcfde4f2eca8b12a4685369ab1989c04e1f Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Sat, 27 Feb 2021 00:54:59 -0500 Subject: [PATCH] PadMessageHandler: Fix fetching of socket.io Sockets for a pad --- src/node/handler/PadMessageHandler.js | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/node/handler/PadMessageHandler.js b/src/node/handler/PadMessageHandler.js index 62d619456..174a6bc5f 100644 --- a/src/node/handler/PadMessageHandler.js +++ b/src/node/handler/PadMessageHandler.js @@ -1409,16 +1409,14 @@ const composePadChangesets = async (padId, startNum, endNum) => { }; const _getRoomSockets = (padID) => { - const roomSockets = []; - const room = socketio.sockets.adapter.rooms[padID]; - - if (room) { - for (const id of Object.keys(room.sockets)) { - roomSockets.push(socketio.sockets.sockets[id]); - } - } - - return roomSockets; + const ns = socketio.sockets; // Default namespace. + const adapter = ns.adapter; + // We could call adapter.clients(), but that method is unnecessarily asynchronous. Replicate what + // it does here, but synchronously to avoid a race condition. This code will have to change when + // we update to socket.io v3. + const room = adapter.rooms[padID]; + if (!room) return []; + return Object.keys(room.sockets).map((id) => ns.connected[id]).filter((s) => s); }; /**