From 4090bbc0c40e586d1c254e97116e352ef4536225 Mon Sep 17 00:00:00 2001 From: Khush Jammu Date: Sat, 6 Jun 2020 13:24:51 +0800 Subject: [PATCH] move disconnect handling to server-side. more robust, as peers can reconnect when appropriate --- public/js/chat.js | 18 ++++++------------ server.js | 6 ++++-- 2 files changed, 10 insertions(+), 14 deletions(-) diff --git a/public/js/chat.js b/public/js/chat.js index 9487a51..b8ae442 100644 --- a/public/js/chat.js +++ b/public/js/chat.js @@ -220,17 +220,11 @@ var VideoChat = { logIt("connected"); break; case "disconnected": - // Remove UUID if connection to server is intact - if (VideoChat.socket.connected) { - VideoChat.onLeave(uuid); - } else { - location.reload(); - } + // Disconnects are handled server-side + logIt("disconnected - UUID " + uuid); break; case "failed": logIt("failed"); - // VideoChat.socket.connect - // VideoChat.createOffer(); // Refresh page if connection has failed location.reload(); break; @@ -849,12 +843,12 @@ function uuidToHue(uuid) { } var hue = Math.abs(hash % 360); // Ensure color is not similar to other colors - var availColors = Array.from({length: 9}, (x,i) => i*40); - VideoChat.peerColors.forEach(function(value, key, map) {availColors[Math.floor(value/40)] = null}); - if (availColors[Math.floor(hue/40)] == null) { + var availColors = Array.from({length: 6}, (x,i) => i*60); + VideoChat.peerColors.forEach(function(value, key, map) {availColors[Math.floor(value/60)] = null}); + if (availColors[Math.floor(hue/60)] == null) { for (var i = 0; i < availColors.length; i++) { if (availColors[i] != null) { - hue = (hue % 40) + availColors[i]; + hue = (hue % 60) + availColors[i]; availColors[i] = null; break; } diff --git a/server.js b/server.js index 64c823d..dd3c3de 100644 --- a/server.js +++ b/server.js @@ -94,8 +94,10 @@ io.on("connection", function (socket) { } }); - socket.on("leave", function (room) { - logIt("A client has left the room", room); + // Client is disconnecting from the server + socket.on('disconnecting', () => { + var room = Object.keys(socket.rooms).filter(item => item != socket.id); // Socket joins a room of itself, remove that + logIt("A client has disconnected from the room", room); socket.broadcast.to(room).emit("leave", socket.id); });