diff --git a/public/webrtc.js b/public/webrtc.js index 30ac9e6..7d0f9c1 100755 --- a/public/webrtc.js +++ b/public/webrtc.js @@ -1,142 +1,147 @@ -var localVideo; -var localStream; -var remoteVideo; -var peerConnection; -var uuid; -var serverConnection; - -var peerConnectionConfig = { - 'iceServers': [ - {'urls': 'stun:stun.stunprotocol.org:3478'}, - {'urls': 'stun:stun.l.google.com:19302'}, - ] -}; - -function getCookie(cname) { - var name = cname + "="; - var decodedCookie = decodeURIComponent(document.cookie); - var ca = decodedCookie.split(';'); - for(var i = 0; i Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1) + this.uuid = s4() + s4() + '-' + s4() + '-' + s4() + '-' + s4() + '-' + s4() + s4() + s4(); + this.set("uuid", this.uuid) + } + return this.uuid + } + + get(k) { + var name = k + "="; + var decodedCookie = decodeURIComponent(document.cookie); + var ca = decodedCookie.split(';'); + for(var i = 0; i this.gotIceCandidate(e); + this.peerConnection.ontrack = (e) => this.gotRemoteStream(e); + this.peerConnection.addStream(this.localStream); + + if(isCaller) { + this.peerConnection + .createOffer({ + 'iceRestart': true, + 'voiceActivityDetection': true, + }) + .then((d) => this.createdDescription(d)) + .catch((m) => this.errorHandler(m)); + } + } + + getUserMediaSuccess(stream) { + this.localVideo = document.getElementById('localVideo'); + this.remoteVideo = document.getElementById('remoteVideo'); + + this.localStream = stream; + this.localVideo.srcObject = stream; + } + + gotMessageFromServer(message) { + if(!this.peerConnection) start(false); + + var signal = JSON.parse(message.data); + + // Ignore messages from ourself + if(signal.uuid == this.id) return; + + if(signal.sdp) { + this.peerConnection.setRemoteDescription(new RTCSessionDescription(signal.sdp)).then(function() { + // Only create answers in response to offers + if(signal.sdp.type == 'offer') { + this.peerConnection + .createAnswer() + .then((d) => this.createdDescription(d)) + .catch((e) => this.errorHandler(e)); + } + }).catch((e) => this.errorHandler(e)); + } else if(signal.ice) { + this.peerConnection + .addIceCandidate(new RTCIceCandidate(signal.ice)) + .catch((e) => this.errorHandler(e)); + } + } + + gotIceCandidate(event) { + if(event.candidate != null) { + this.serverConnection + .send(JSON.stringify({ + 'ice': event.candidate, + 'uuid': this.id, + })); + } + } + + createdDescription(description) { + console.log('got description'); + + this.peerConnection + .setLocalDescription(description) + .then(() => { + this.serverConnection.send(JSON.stringify({ + 'sdp': this.peerConnection.localDescription, + 'uuid': this.id, + })); + }) + .catch((e) => this.errorHandler(e)); + } + + gotRemoteStream(event) { + console.log('got remote stream'); + this.remoteVideo.srcObject = event.streams[0]; + } + + errorHandler(error) { + console.log(error); + } +} + +var self + +function pageReady() { + self = new Self() +} + +function start(isCaller) { + self.start(isCaller) +} diff --git a/ws.go b/ws.go index f63f1b3..f331892 100644 --- a/ws.go +++ b/ws.go @@ -3,6 +3,7 @@ package main import ( "log" "net/http" + "strings" "sync" "github.com/google/uuid" @@ -33,10 +34,11 @@ func (ws *WS) ServeHTTP(w http.ResponseWriter, r *http.Request) { } func (ws *WS) serveHTTP(w http.ResponseWriter, r *http.Request) error { - id := r.URL.Query().Get("uuid") + id := strings.Split(r.Header.Get("Cookie"), "=")[1] if len(id) == 0 { id = uuid.New().String() } + log.Println("found id", id) log.Println("ws serve http", r.URL.Path) pooli, ok := ws.pools.Load(r.URL.Path) if !ok {