X-Git-Url: https://git.kengrimes.com/?p=henge%2Fkiak.git;a=blobdiff_plain;f=host.js;fp=host.js;h=811c4670ca9a24ef09b0a4aa4555f56c0ef8002a;hp=d2808121ce56f2f341fffdda321c2e40ecd4258c;hb=3f5fce84645ec2c59aec0908807d1e9cb3e05e2d;hpb=68d808b03537c8cbe71d895f2ebc0bd9ab232bea diff --git a/host.js b/host.js index d280812..811c467 100644 --- a/host.js +++ b/host.js @@ -7,7 +7,7 @@ const iceCandidates = [] let dataChannel let screenStream /* TODO: Remove if can access localStreams */ let tracks = [] - +let hpk /* TODO: duplicate in both client.js and host.jhs */ function getPublicKey() { @@ -42,6 +42,18 @@ function getPublicKey() { }) } +function sendClientICE(clientPubKey, hostPubKey, iceCandidate) { + console.log('Host: Allocating ice candidate for client') + console.log(iceCandidate) + iceCandidates.push(JSON.stringify({ + cmd: "< ice pubKey", + ice: iceCandidate, + hostPubKey: hostPubKey, /* TODO: do we need to send this? */ + clientPubKey: clientPubKey, + iceState: "a" + })) +} + function handleNewClientConnection(offer) { /* New Client Connection*/ hpc = new RTCPeerConnection(conf) @@ -53,33 +65,23 @@ function handleNewClientConnection(offer) { return hpc.setLocalDescription(answer) }) .then(() => { - getPublicKey().then((hpk) => { - hpc['hpk'] = hpk - hpc.onicecandidate = (event) => { - if (event.candidate) { - console.log('Host: Allocating ice candidate for client') - iceCandidates.push(JSON.stringify({ - cmd: "< ice pubKey", - ice: event.candidate, - hostPubKey: hpk.n, /* TODO: do we need to send this? */ - clientPubKey: offer.pubKey, - iceState: "a" - })) - } - else { - console.log('Host: Finished sending ICE candidates') - } - } - console.log('Host: Sending answer to Client ') - wsock.send(JSON.stringify({ - cmd: '< sdp pubKey', - sdp: hpc.localDescription, - hostPubKey: hpk.n, - clientPubKey: offer.pubKey - })) + hpc.onicecandidate = (event) => { + if (event.candidate) { + sendClientICE(offer.pubKey, hpk.n, event.candidate) + } + else { + console.log('Host: Finished allocating ICE candidates') + } + } + console.log('Host: Sending answer to Client ') + wsock.send(JSON.stringify({ + cmd: '< sdp pubKey', + sdp: hpc.localDescription, + hostPubKey: hpk.n, + clientPubKey: offer.pubKey + })) - }) }).catch((err) => { console.log(`error in host answer ${err}`) }) @@ -91,12 +93,13 @@ function handleNewClientConnection(offer) { dataChannel = evt.channel dataChannel.onmessage = (msg) => { let clientMessage = JSON.parse(msg.data) - console.log(clientMessage) + console.log(`client message is ${clientMessage}`) hpc.setRemoteDescription(clientMessage.sdp).then(() => { console.log('should be streaming now') }) } dataChannel.onopen = () => { + /* If !screenStream, gUM */ screenStream.getTracks().forEach( (track) => { hpc.addTrack(track, screenStream) }) @@ -168,6 +171,10 @@ if ("WebSocket" in window) { console.log(err); }); // always check for errors at the end. + getPublicKey() + .then((hpkVal) => { + hpk = hpkVal + }) wsock = new WebSocket(`${_strapp_protocol}://${window.location.hostname}:${_strapp_port}`) wsock.onopen = () => { console.log(`Strapped to ${_strapp_protocol}://${window.location.hostname}:${_strapp_port}`) @@ -177,6 +184,7 @@ if ("WebSocket" in window) { /* msg is either offer or ice candidate or ice candidate request*/ /* What if data null? */ + console.log(`serverMsg = ${serverMsg.data}`) let msg = JSON.parse(serverMsg.data) const clientID = msg.pubKey