diff --git a/public/js/chat.js b/public/js/chat.js index 3c3e49c..dc50958 100644 --- a/public/js/chat.js +++ b/public/js/chat.js @@ -1,7 +1,6 @@ // Vars var isMuted; var videoIsPaused; -var dataChanel = null; const browserName = getBrowserName(); const url = window.location.href; const roomHash = url.substring(url.lastIndexOf("/") + 1).toLowerCase(); @@ -27,13 +26,18 @@ const captionButtontext = $("#caption-button-text"); const entireChat = $("#entire-chat"); const chatZone = $("#chat-zone"); +var dataChannel = new Map(); + var VideoChat = { - connected: [], + nickname: undefined, + videoEnabled: true, + audioEnabled: true, + connected: new Map(), localICECandidates: {}, socket: io(), remoteVideoWrapper: document.getElementById("wrapper"), localVideo: document.getElementById("local-video"), - peerConnections: {}, + peerConnections: new Map(), recognition: undefined, // Call to getUserMedia (provided by adapter.js for cross browser compatibility) @@ -91,6 +95,16 @@ var VideoChat = { Snackbar.close(); }, }); + + VideoChat.nickname = prompt("Please enter a nickname", ""); + + while (VideoChat.nickname == null || VideoChat.nickname.trim() === "") { + VideoChat.nickname = prompt( + "Nickname cannot be empty. Please enter a nickname", + "" + ); + } + VideoChat.localVideo.srcObject = stream; // Now we're ready to join the chat room. VideoChat.socket.emit("join", roomHash); @@ -107,7 +121,6 @@ var VideoChat = { ); }, - call: function (uuid, room) { logIt("Initiating call with " + uuid); VideoChat.socket.on( @@ -127,29 +140,36 @@ var VideoChat = { console.log("establishing connection to", uuid); // Initialise localICEcandidates for node uuid with empty array VideoChat.localICECandidates[uuid] = []; - VideoChat.connected[uuid] = false; - + VideoChat.connected.set(uuid, false); // Set up a new RTCPeerConnection using the token's iceServers. - VideoChat.peerConnections[uuid] = new RTCPeerConnection({ - iceServers: token.iceServers, - }); + VideoChat.peerConnections.set( + uuid, + new RTCPeerConnection({ + iceServers: token.iceServers, + }) + ); // Add the local video stream to the peerConnection. VideoChat.localStream.getTracks().forEach(function (track) { - VideoChat.peerConnections[uuid].addTrack(track, VideoChat.localStream); + VideoChat.peerConnections + .get(uuid) + .addTrack(track, VideoChat.localStream); }); // Add general purpose data channel to peer connection, // used for text chats, captions, and toggling sending captions - dataChanel = VideoChat.peerConnections[uuid].createDataChannel("chat", { - negotiated: true, - // both peers must have same id - id: 0, - }); + dataChannel.set( + uuid, + VideoChat.peerConnections.get(uuid).createDataChannel("chat", { + negotiated: true, + // both peers must have same id + id: 0, + }) + ); // Called when dataChannel is successfully opened - dataChanel.onopen = function (event) { + dataChannel.get(uuid).onopen = function (event) { logIt("dataChannel opened"); }; // Handle different dataChannel types - dataChanel.onmessage = function (event) { + dataChannel.get(uuid).onmessage = function (event) { const receivedData = event.data; // First 4 chars represent data type const dataType = receivedData.substring(0, 4); @@ -165,18 +185,18 @@ var VideoChat = { // Set up callbacks for the connection generating iceCandidates or // receiving the remote media stream. - VideoChat.peerConnections[uuid].onicecandidate = function (u) { + VideoChat.peerConnections.get(uuid).onicecandidate = function (u) { VideoChat.onIceCandidate(u, uuid); }; - VideoChat.peerConnections[uuid].onaddstream = function (u) { + VideoChat.peerConnections.get(uuid).onaddstream = function (u) { VideoChat.onAddStream(u, uuid); }; // Called when there is a change in connection state - VideoChat.peerConnections[uuid].oniceconnectionstatechange = function ( - event - ) { - switch (VideoChat.peerConnections[uuid].iceConnectionState) { + VideoChat.peerConnections.get( + uuid + ).oniceconnectionstatechange = function (event) { + switch (VideoChat.peerConnections.get(uuid).iceConnectionState) { case "connected": logIt("connected"); // Once connected we no longer have a need for the signaling server, so disconnect @@ -206,7 +226,7 @@ var VideoChat = { logIt( `<<< Received local ICE candidate from STUN/TURN server (${event.candidate.address}) associated with UUID (${uuid})` ); - if (VideoChat.connected[uuid]) { + if (VideoChat.connected.get(uuid)) { logIt(`>>> Sending local ICE candidate (${event.candidate.address})`); VideoChat.socket.emit( "candidate", @@ -233,18 +253,18 @@ var VideoChat = { logIt( `onCandidate <<< Received remote ICE candidate (${rtcCandidate.address} - ${rtcCandidate.relatedAddress})` ); - VideoChat.peerConnections[uuid].addIceCandidate(rtcCandidate); + VideoChat.peerConnections.get(uuid).addIceCandidate(rtcCandidate); }, // Create an offer that contains the media capabilities of the browser. createOffer: function (uuid) { logIt(`createOffer to ${uuid} >>> Creating offer...`); - VideoChat.peerConnections[uuid].createOffer( + VideoChat.peerConnections.get(uuid).createOffer( function (offer) { // If the offer is created successfully, set it as the local description // and send it over the socket connection to initiate the peerConnection // on the other side. - VideoChat.peerConnections[uuid].setLocalDescription(offer); + VideoChat.peerConnections.get(uuid).setLocalDescription(offer); VideoChat.socket.emit("offer", JSON.stringify(offer), roomHash, uuid); }, function (err) { @@ -263,10 +283,10 @@ var VideoChat = { logIt("createAnswer"); rtcOffer = new RTCSessionDescription(JSON.parse(offer)); logIt(`>>> Creating answer to ${uuid}`); - VideoChat.peerConnections[uuid].setRemoteDescription(rtcOffer); - VideoChat.peerConnections[uuid].createAnswer( + VideoChat.peerConnections.get(uuid).setRemoteDescription(rtcOffer); + VideoChat.peerConnections.get(uuid).createAnswer( function (answer) { - VideoChat.peerConnections[uuid].setLocalDescription(answer); + VideoChat.peerConnections.get(uuid).setLocalDescription(answer); VideoChat.socket.emit("answer", JSON.stringify(answer), roomHash, uuid); }, function (err) { @@ -295,7 +315,7 @@ var VideoChat = { logIt(`onAnswer <<< Received answer from ${uuid}`); var rtcAnswer = new RTCSessionDescription(JSON.parse(answer)); // Set remote description of RTCSession - VideoChat.peerConnections[uuid].setRemoteDescription(rtcAnswer); + VideoChat.peerConnections.get(uuid).setRemoteDescription(rtcAnswer); // The caller now knows that the callee is ready to accept new ICE candidates, so sending the buffer over VideoChat.localICECandidates[uuid].forEach((candidate) => { logIt(`>>> Sending local ICE candidate (${candidate.address})`); @@ -311,8 +331,7 @@ var VideoChat = { // Called when a stream is added to the peer connection onAddStream: function (event, uuid) { - logIt( - "onAddStream <<< Received new stream from remote. Adding it..."); + logIt("onAddStream <<< Received new stream from remote. Adding it..."); // Create new remote video source in wrapper // Create a