Turn call an OK
parent
e1ee647767
commit
5418ef2b37
|
|
@ -1,20 +1,4 @@
|
|||
class Config {
|
||||
static iceConfig = {
|
||||
'iceServers': [
|
||||
{'urls': 'stun:stun.stunprotocol.org:3478'},
|
||||
{'urls': 'stun:stun.l.google.com:19302'},
|
||||
/*
|
||||
{
|
||||
'urls': 'turn:turn.home.blapointe.com:5349',
|
||||
'credentialType': 'password',
|
||||
'username': 'user',
|
||||
'credential': 'pass',
|
||||
},
|
||||
*/
|
||||
],
|
||||
'iceTransportPolicy': 'all', // all for p2p, relay for turn
|
||||
};
|
||||
|
||||
static getCookie(cname) {
|
||||
var name = cname + "=";
|
||||
var decodedCookie = decodeURIComponent(document.cookie);
|
||||
|
|
@ -30,14 +14,14 @@ class Config {
|
|||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
|
||||
static setCookie(cname, cvalue) {
|
||||
var d = new Date();
|
||||
d.setTime(d.getTime() + (1*24*60*60*1000));
|
||||
var expires = "expires="+ d.toUTCString();
|
||||
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
|
||||
}
|
||||
|
||||
|
||||
static getUUID() {
|
||||
var uuid = Config.getCookie('uuid');
|
||||
if (!uuid) {
|
||||
|
|
@ -46,22 +30,33 @@ class Config {
|
|||
}
|
||||
return uuid;
|
||||
}
|
||||
|
||||
|
||||
static createUUID() {
|
||||
return "" + new Date();
|
||||
function s4() {
|
||||
return Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1);
|
||||
}
|
||||
|
||||
|
||||
return s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4();
|
||||
}
|
||||
}
|
||||
Config.iceConfig = {
|
||||
'iceServers': [
|
||||
{
|
||||
'urls': 'turn:turn.home.blapointe.com:5349',
|
||||
'credentialType': 'password',
|
||||
'username': 'user',
|
||||
'credential': 'pass',
|
||||
},
|
||||
],
|
||||
'iceTransportPolicy': 'relay',
|
||||
};
|
||||
|
||||
class View {
|
||||
static write(foo, color) {
|
||||
var msg = [].slice.call(arguments[2]).join(" ");
|
||||
foo(msg);
|
||||
document.getElementById("log").innerHTML +=
|
||||
document.getElementById("log").innerHTML +=
|
||||
'<br><span style="color: ' + color + ';">' + msg + '</span>';
|
||||
}
|
||||
|
||||
|
|
@ -101,7 +96,7 @@ class Preview {
|
|||
}
|
||||
|
||||
class Server {
|
||||
static server;
|
||||
server = null;
|
||||
|
||||
constructor(address, cb) {
|
||||
Server.server = new WebSocket(address);
|
||||
|
|
@ -110,8 +105,6 @@ class Server {
|
|||
}
|
||||
|
||||
class Peer {
|
||||
peer;
|
||||
|
||||
constructor(stream) {
|
||||
this.peer = new RTCPeerConnection(Config.iceConfig);
|
||||
this.peer.onicecandidate = this.gotIceCandidate;
|
||||
|
|
@ -135,30 +128,28 @@ class Peer {
|
|||
.catch(View.error);
|
||||
}
|
||||
}
|
||||
Peer.peer = null;
|
||||
|
||||
class Entropy {
|
||||
static local = null;
|
||||
static peer = null;
|
||||
|
||||
static pageReady() {
|
||||
Entropy.local = Local.build("video");
|
||||
}
|
||||
|
||||
|
||||
static start(isCaller) {
|
||||
Entropy.peer = new Peer(Entropy.local.stream);
|
||||
if(isCaller) {
|
||||
Entropy.peer.offer(Entropy.createdDescription);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static gotMessageFromServer(message) {
|
||||
if(!Entropy.peer.peer) Entropy.start(false);
|
||||
|
||||
if(!Entropy.peer || !Entropy.peer.peer) Entropy.start(false);
|
||||
|
||||
var signal = JSON.parse(message.data);
|
||||
|
||||
|
||||
// Ignore messages from ourself
|
||||
if(signal.uuid == Config.getUUID()) return;
|
||||
|
||||
|
||||
if(signal.sdp) {
|
||||
Entropy.peer.peer.setRemoteDescription(new RTCSessionDescription(signal.sdp)).then(function() {
|
||||
// Only create answers in response to offers
|
||||
|
|
@ -170,21 +161,23 @@ class Entropy {
|
|||
Entropy.peer.peer.addIceCandidate(new RTCIceCandidate(signal.ice)).catch(View.error);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
static createdDescription(description) {
|
||||
View.log('got description');
|
||||
|
||||
|
||||
Entropy.peer.peer
|
||||
.setLocalDescription(description)
|
||||
.then(function() {
|
||||
Server.server.send(JSON.stringify({
|
||||
'sdp': Entropy.peer.peer.localDescription,
|
||||
'sdp': Entropy.peer.peer.localDescription,
|
||||
'uuid': Config.getUUID(),
|
||||
}));
|
||||
})
|
||||
.catch(View.error);
|
||||
}
|
||||
}
|
||||
Entropy.local = null;
|
||||
Entropy.peer = null;
|
||||
|
||||
class Local {
|
||||
stream = null;
|
||||
|
|
@ -250,7 +243,11 @@ window.console = console;
|
|||
|
||||
function pageReady() {
|
||||
Config.getUUID();
|
||||
new Server('wss://' + window.location.hostname + '/abc', Entropy.gotMessageFromServer);
|
||||
var host = window.location.hostname;
|
||||
if (window.location.port) {
|
||||
host += ":" + window.location.port;
|
||||
}
|
||||
new Server('wss://' + host + '/abc', Entropy.gotMessageFromServer);
|
||||
Entropy.pageReady();
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -30,11 +30,10 @@ func New() *Server {
|
|||
}
|
||||
|
||||
func (s *Server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
log.Println("base", path.Base(r.URL.Path))
|
||||
if !s.Authorize(w, r) {
|
||||
return
|
||||
}
|
||||
|
||||
log.Println("ext", path.Ext(r.URL.Path))
|
||||
if path.Ext(r.URL.Path) != "" {
|
||||
s.fs.ServeHTTP(w, r)
|
||||
} else if _, err := os.Stat(path.Join(config().GetString("d"), r.URL.Path[1:])); os.IsNotExist(err) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue