From 572bab2f00995891184d5368fbdf1938907fb6af Mon Sep 17 00:00:00 2001 From: bel Date: Sun, 10 May 2020 11:28:55 -0600 Subject: [PATCH] move out config into own class --- public/webrtc.js | 67 ++++++++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 31 deletions(-) diff --git a/public/webrtc.js b/public/webrtc.js index 7d0f9c1..6966030 100755 --- a/public/webrtc.js +++ b/public/webrtc.js @@ -1,10 +1,4 @@ -class Entropy { - static localVideo = null; - static localStream = null; - static remoteVideo = null; - static peerConnection = null; - static uuid = null; - static serverConnection = null; +class Config { static peerConnectionConfig = { 'iceServers': [ {'urls': 'stun:stun.stunprotocol.org:3478'}, @@ -35,13 +29,38 @@ class Entropy { document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/"; } - static pageReady() { - Entropy.uuid = Entropy.getCookie("uuid"); - if (!Entropy.uuid) { - Entropy.uuid = Entropy.createUUID(); - Entropy.setCookie("uuid", Entropy.uuid); + static getUUID() { + var uuid = Config.getCookie('uuid'); + if (!uuid) { + uuid = Config.createUUID(); + Config.setCookie('uuid'); + } + return uuid; + } + + static createUUID() { + function s4() { + return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1); } + return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4(); + } +} + +function pageReady() { + Entropy.pageReady(); +} + +class Entropy { + static localVideo = null; + static localStream = null; + static remoteVideo = null; + static peerConnection = null; + static serverConnection = null; + + static pageReady() { + Config.getUUID(); + Entropy.localVideo = document.getElementById('localVideo'); Entropy.remoteVideo = document.getElementById('remoteVideo'); @@ -50,7 +69,7 @@ class Entropy { var constraints = { video: true, - audio: false, + audio: true, }; if(navigator.mediaDevices.getUserMedia) { @@ -66,7 +85,7 @@ class Entropy { } static start(isCaller) { - Entropy.peerConnection = new RTCPeerConnection(Entropy.peerConnectionConfig); + Entropy.peerConnection = new RTCPeerConnection(Config.peerConnectionConfig); Entropy.peerConnection.onicecandidate = Entropy.gotIceCandidate; Entropy.peerConnection.ontrack = Entropy.gotRemoteStream; Entropy.peerConnection.addStream(Entropy.localStream); @@ -88,7 +107,7 @@ class Entropy { var signal = JSON.parse(message.data); // Ignore messages from ourself - if(signal.uuid == Entropy.uuid) return; + if(signal.uuid == Config.getUUID()) return; if(signal.sdp) { Entropy.peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp)).then(function() { @@ -104,7 +123,7 @@ class Entropy { static gotIceCandidate(event) { if(event.candidate != null) { - Entropy.serverConnection.send(JSON.stringify({'ice': event.candidate, 'uuid': Entropy.uuid})); + Entropy.serverConnection.send(JSON.stringify({'ice': event.candidate, 'uuid': Config.getUUID()})); } } @@ -116,7 +135,7 @@ class Entropy { .then(function() { Entropy.serverConnection.send(JSON.stringify({ 'sdp': Entropy.peerConnection.localDescription, - 'uuid': Entropy.uuid, + 'uuid': Config.getUUID(), })); }) .catch(errorHandler); @@ -130,18 +149,4 @@ class Entropy { static errorHandler(error) { console.log(error); } - - // Taken from http://stackoverflow.com/a/105074/515584 - // Strictly speaking, it's not a real UUID, but it gets the job done here - static createUUID() { - function s4() { - return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1); - } - - return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4(); - } -} - -function pageReady() { - Entropy.pageReady(); }