mirror of
https://github.com/ianramzy/decentralized-video-chat.git
synced 2024-11-17 07:39:21 +08:00
Added graceful disconnect back
This commit is contained in:
parent
624c9ee94f
commit
09bc7afec6
@ -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});
|
||||||
|
@ -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) {
|
||||||
|
Loading…
Reference in New Issue
Block a user