move out config into own class

master
bel 2020-05-10 11:28:55 -06:00
parent 4fdb421204
commit 572bab2f00
1 changed files with 36 additions and 31 deletions

View File

@ -1,10 +1,4 @@
class Entropy { class Config {
static localVideo = null;
static localStream = null;
static remoteVideo = null;
static peerConnection = null;
static uuid = null;
static serverConnection = null;
static peerConnectionConfig = { static peerConnectionConfig = {
'iceServers': [ 'iceServers': [
{'urls': 'stun:stun.stunprotocol.org:3478'}, {'urls': 'stun:stun.stunprotocol.org:3478'},
@ -35,13 +29,38 @@ class Entropy {
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/"; document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
} }
static pageReady() { static getUUID() {
Entropy.uuid = Entropy.getCookie("uuid"); var uuid = Config.getCookie('uuid');
if (!Entropy.uuid) { if (!uuid) {
Entropy.uuid = Entropy.createUUID(); uuid = Config.createUUID();
Entropy.setCookie("uuid", Entropy.uuid); 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.localVideo = document.getElementById('localVideo');
Entropy.remoteVideo = document.getElementById('remoteVideo'); Entropy.remoteVideo = document.getElementById('remoteVideo');
@ -50,7 +69,7 @@ class Entropy {
var constraints = { var constraints = {
video: true, video: true,
audio: false, audio: true,
}; };
if(navigator.mediaDevices.getUserMedia) { if(navigator.mediaDevices.getUserMedia) {
@ -66,7 +85,7 @@ class Entropy {
} }
static start(isCaller) { static start(isCaller) {
Entropy.peerConnection = new RTCPeerConnection(Entropy.peerConnectionConfig); Entropy.peerConnection = new RTCPeerConnection(Config.peerConnectionConfig);
Entropy.peerConnection.onicecandidate = Entropy.gotIceCandidate; Entropy.peerConnection.onicecandidate = Entropy.gotIceCandidate;
Entropy.peerConnection.ontrack = Entropy.gotRemoteStream; Entropy.peerConnection.ontrack = Entropy.gotRemoteStream;
Entropy.peerConnection.addStream(Entropy.localStream); Entropy.peerConnection.addStream(Entropy.localStream);
@ -88,7 +107,7 @@ class Entropy {
var signal = JSON.parse(message.data); var signal = JSON.parse(message.data);
// Ignore messages from ourself // Ignore messages from ourself
if(signal.uuid == Entropy.uuid) return; if(signal.uuid == Config.getUUID()) return;
if(signal.sdp) { if(signal.sdp) {
Entropy.peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp)).then(function() { Entropy.peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp)).then(function() {
@ -104,7 +123,7 @@ class Entropy {
static gotIceCandidate(event) { static gotIceCandidate(event) {
if(event.candidate != null) { 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() { .then(function() {
Entropy.serverConnection.send(JSON.stringify({ Entropy.serverConnection.send(JSON.stringify({
'sdp': Entropy.peerConnection.localDescription, 'sdp': Entropy.peerConnection.localDescription,
'uuid': Entropy.uuid, 'uuid': Config.getUUID(),
})); }));
}) })
.catch(errorHandler); .catch(errorHandler);
@ -130,18 +149,4 @@ class Entropy {
static errorHandler(error) { static errorHandler(error) {
console.log(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();
} }