From 4487ee2d68a62278fbadbe8f018cc6cd9a08062e Mon Sep 17 00:00:00 2001 From: Adam Soltys Date: Thu, 8 Oct 2020 19:14:50 -0700 Subject: prevent double broadcast --- lib/sockets.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/sockets.js b/lib/sockets.js index fcca9386..1ed18610 100644 --- a/lib/sockets.js +++ b/lib/sockets.js @@ -34,15 +34,17 @@ emit = (username, type, data) => { }; broadcast = (type, data) => { + const sent = []; for (const username in sockets) { for (const id in sockets[username]) { const ws = sockets[username][id]; ws.send(JSON.stringify({ type, data })); + sent.push(ws.id); } } Object.values(all).map(ws => { - ws.send(JSON.stringify({ type, data })); + if (!sent.includes(ws.id)) ws.send(JSON.stringify({ type, data })); }); }; @@ -66,6 +68,7 @@ setInterval(() => { wss.on("connection", function(ws, req) { const id = uuidv4(); + ws.id = id; ws.beats = 0; ws.send(JSON.stringify({ type: "connected", data: id })); all[id] = ws; -- cgit v1.2.3