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 {
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,12 +29,37 @@ 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();
}