From 3f5fce84645ec2c59aec0908807d1e9cb3e05e2d Mon Sep 17 00:00:00 2001 From: Jordan Date: Mon, 10 Jul 2017 20:40:35 +0000 Subject: [PATCH] updating with new router.js --- client.js | 12 +++++++---- host.js | 62 +++++++++++++++++++++++++++++++------------------------ 2 files changed, 43 insertions(+), 31 deletions(-) diff --git a/client.js b/client.js index 160d5dd..c1956a7 100644 --- a/client.js +++ b/client.js @@ -5,7 +5,7 @@ const conf = {"iceServers": [{ "urls": "stun:stun.1.google.com:19302" }] } let dataChannel let hostScreen -/* TODO: duplicate in both client.js and host.js */ +/* TODO: This is duplicated in both client.js and host.js */ function getPublicKey() { return new Promise( (resolve, reject) => { /* Check local storage for public key */ @@ -55,7 +55,7 @@ function requestHostAnswer(url, data) { /* But there is no JSON? */ request.setRequestHeader('Content-Type', 'application/json' ) request.setRequestHeader('X-Strapp-Type', 'client-sdp-offer') - request.setRequestHeader('X-Client-Offer', JSON.stringify(data)) + request.setRequestHeader('X-Strapp-Pubkey', data.pubKey) request.onreadystatechange = () => { if (request.status === 200) { if(request.readyState === 4) { @@ -72,7 +72,7 @@ function requestHostAnswer(url, data) { reject('server unhandled response of status ' + request.status) } } - request.send() + request.send(data) }) } @@ -170,8 +170,10 @@ getPublicKey().then((cpk) => { console.log('error in sdp handshake: ' + err) }) } - /* Start data channel */ + /* Start data channel, triggers on negotiation needed */ dataChannel = cpc.createDataChannel("sendChannel"); + + /* Triggered when Host adds track to peer connection */ cpc.ontrack = (event) => { console.log(`track event is ${event}`) let remoteRTPSenders = cpc.getSenders() @@ -192,9 +194,11 @@ getPublicKey().then((cpk) => { video.play() } } + dataChannel.onmessage = (msg) => { /* Get mediaStream from host and add it to the video */ let hostMessage = JSON.parse(msg.data) + console.log('Client: Renego') cpc.setRemoteDescription(hostMessage.sdp).then(() => { cpc.createAnswer().then((answer) => { return cpc.setLocalDescription(answer) 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 -- 2.18.0