Added graceful disconnect back

This commit is contained in:
Arya Vohra 2020-06-05 17:55:19 +08:00
parent 624c9ee94f
commit 09bc7afec6
2 changed files with 26 additions and 9 deletions

View File

@ -107,6 +107,7 @@ var VideoChat = {
VideoChat.socket.emit("join", roomHash); VideoChat.socket.emit("join", roomHash);
// Add listeners to the websocket // Add listeners to the websocket
VideoChat.socket.on("leave", VideoChat.onLeave);
VideoChat.socket.on("full", chatRoomFull); VideoChat.socket.on("full", chatRoomFull);
VideoChat.socket.on("offer", VideoChat.onOffer); VideoChat.socket.on("offer", VideoChat.onOffer);
VideoChat.socket.on("willInitiateCall", VideoChat.call); VideoChat.socket.on("willInitiateCall", VideoChat.call);
@ -131,6 +132,21 @@ var VideoChat = {
VideoChat.socket.emit("token", roomHash, uuid); VideoChat.socket.emit("token", roomHash, uuid);
}, },
onLeave: function(uuid) {
logIt("disconnected - UUID " + uuid);
// Remove video element
VideoChat.remoteVideoWrapper.removeChild(
document.querySelectorAll(`[uuid="${uuid}"]`)[0]
);
// Delete connection & metadata
VideoChat.connected.delete(uuid);
VideoChat.peerConnections.delete(uuid);
dataChannel.delete(uuid);
if (VideoChat.peerConnections.size === 0) {
displayWaitingCaption();
}
},
establishConnection: function (correctUuid, callback) { establishConnection: function (correctUuid, callback) {
return function (token, uuid) { return function (token, uuid) {
if (correctUuid != uuid) { if (correctUuid != uuid) {
@ -203,14 +219,11 @@ var VideoChat = {
logIt("connected"); logIt("connected");
break; break;
case "disconnected": case "disconnected":
logIt("disconnected - UUID " + uuid); // Remove UUID if connection to server is intact
VideoChat.remoteVideoWrapper.removeChild(document.querySelectorAll(`[uuid="${uuid}"]`)[0]); if (VideoChat.socket.connected) {
VideoChat.connected.delete(uuid); VideoChat.onLeave(uuid);
VideoChat.peerConnections.delete(uuid); } else {
dataChannel.delete(uuid); location.reload();
if (VideoChat.peerConnections.size === 0) {
displayWaitingCaption();
} }
break; break;
case "failed": case "failed":
@ -867,7 +880,6 @@ function uuidToColor(uuid) {
hash = hash & hash; hash = hash & hash;
} }
var hue = Math.abs(hash % 360); var hue = Math.abs(hash % 360);
console.log(hue);
// Ensure color is not similar to other colors // Ensure color is not similar to other colors
var availColors = Array.from({length: 18}, (x,i) => i*20); var availColors = Array.from({length: 18}, (x,i) => i*20);
VideoChat.peerColors.forEach(function(value, key, map) {availColors[Math.floor(value/20)] = null}); VideoChat.peerColors.forEach(function(value, key, map) {availColors[Math.floor(value/20)] = null});

View File

@ -92,6 +92,11 @@ io.on("connection", function (socket) {
} }
}); });
socket.on("leave", function (room) {
logIt("A client has left the room", room);
socket.broadcast.to(room).emit("leave", socket.id);
});
// When receiving the token message, use the Twilio REST API to request an // When receiving the token message, use the Twilio REST API to request an
// token to get ephemeral credentials to use the TURN server. // token to get ephemeral credentials to use the TURN server.
socket.on("token", function (room, uuid) { socket.on("token", function (room, uuid) {