updating with new router.js
authorJordan <jordanlavatai@gmail.com>
Mon, 10 Jul 2017 20:40:35 +0000 (20:40 +0000)
committerJordan <jordanlavatai@gmail.com>
Mon, 10 Jul 2017 20:40:35 +0000 (20:40 +0000)
client.js
host.js

index 160d5dd..c1956a7 100644 (file)
--- a/client.js
+++ b/client.js
@@ -5,7 +5,7 @@ const conf = {"iceServers": [{ "urls": "stun:stun.1.google.com:19302" }] }
 let dataChannel\r
 let hostScreen\r
 \r
-/* TODO: duplicate in both client.js and host.js */\r
+/* TODO: This is duplicated in both client.js and host.js */\r
 function getPublicKey() {\r
   return new Promise( (resolve, reject) => {\r
     /* Check local storage for public key */\r
@@ -55,7 +55,7 @@ function requestHostAnswer(url, data) {
     /* But there is no JSON? */\r
     request.setRequestHeader('Content-Type', 'application/json' )\r
     request.setRequestHeader('X-Strapp-Type', 'client-sdp-offer')\r
-    request.setRequestHeader('X-Client-Offer', JSON.stringify(data))\r
+    request.setRequestHeader('X-Strapp-Pubkey', data.pubKey)    \r
     request.onreadystatechange = () => {\r
       if (request.status === 200) {\r
         if(request.readyState === 4) {\r
@@ -72,7 +72,7 @@ function requestHostAnswer(url, data) {
         reject('server unhandled response of status ' + request.status)\r
       }\r
     }\r
-    request.send()\r
+    request.send(data)\r
   })\r
 }\r
 \r
@@ -170,8 +170,10 @@ getPublicKey().then((cpk) => {
       console.log('error in sdp handshake: ' + err)\r
     })\r
   }\r
-  /* Start data channel */\r
+  /* Start data channel, triggers on negotiation needed */\r
   dataChannel = cpc.createDataChannel("sendChannel");\r
+\r
+  /* Triggered when Host adds track to peer connection */\r
   cpc.ontrack = (event) => {\r
     console.log(`track event is ${event}`)\r
     let remoteRTPSenders = cpc.getSenders()\r
@@ -192,9 +194,11 @@ getPublicKey().then((cpk) => {
       video.play()\r
     }\r
   }\r
+\r
   dataChannel.onmessage = (msg) => {\r
     /* Get mediaStream from host and add it to the video */\r
     let hostMessage = JSON.parse(msg.data)\r
+    console.log('Client: Renego')\r
     cpc.setRemoteDescription(hostMessage.sdp).then(() => {\r
       cpc.createAnswer().then((answer) => {\r
         return cpc.setLocalDescription(answer)\r
diff --git a/host.js b/host.js
index d280812..811c467 100644 (file)
--- a/host.js
+++ b/host.js
@@ -7,7 +7,7 @@ const iceCandidates = []
 let dataChannel\r
 let screenStream /* TODO: Remove if can access localStreams */\r
 let tracks = []\r
-\r
+let hpk\r
 \r
 /* TODO: duplicate in both client.js and host.jhs */\r
 function getPublicKey() {\r
@@ -42,6 +42,18 @@ function getPublicKey() {
   })\r
 }\r
 \r
+function sendClientICE(clientPubKey, hostPubKey, iceCandidate) {\r
+  console.log('Host: Allocating ice candidate for client')\r
+  console.log(iceCandidate)\r
+  iceCandidates.push(JSON.stringify({\r
+    cmd: "< ice pubKey",\r
+    ice: iceCandidate,\r
+    hostPubKey: hostPubKey, /* TODO: do we need to send this? */\r
+    clientPubKey: clientPubKey,\r
+    iceState: "a"\r
+  }))\r
+}\r
+\r
 function handleNewClientConnection(offer) {\r
   /* New Client Connection*/\r
   hpc = new RTCPeerConnection(conf)\r
@@ -53,33 +65,23 @@ function handleNewClientConnection(offer) {
       return hpc.setLocalDescription(answer)\r
     })\r
     .then(() => {\r
-      getPublicKey().then((hpk) => {\r
-        hpc['hpk'] = hpk\r
-        hpc.onicecandidate = (event) => {\r
-          if (event.candidate) {\r
-            console.log('Host: Allocating ice candidate for client')\r
-            iceCandidates.push(JSON.stringify({\r
-              cmd: "< ice pubKey",\r
-              ice: event.candidate,\r
-              hostPubKey: hpk.n, /* TODO: do we need to send this? */\r
-              clientPubKey: offer.pubKey,\r
-              iceState: "a"\r
-            }))\r
-          }\r
-          else {\r
-            console.log('Host: Finished sending ICE candidates')\r
-          }\r
-        }\r
-        console.log('Host: Sending answer to Client ')\r
-        wsock.send(JSON.stringify({\r
-          cmd: '< sdp pubKey',\r
-          sdp: hpc.localDescription,\r
-          hostPubKey: hpk.n,\r
-          clientPubKey: offer.pubKey\r
-        }))\r
 \r
+      hpc.onicecandidate = (event) => {\r
+        if (event.candidate) {\r
+          sendClientICE(offer.pubKey, hpk.n, event.candidate)\r
+        }\r
+        else {\r
+          console.log('Host: Finished allocating ICE candidates')\r
+        }\r
+      }\r
+      console.log('Host: Sending answer to Client ')\r
+      wsock.send(JSON.stringify({\r
+        cmd: '< sdp pubKey',\r
+        sdp: hpc.localDescription,\r
+        hostPubKey: hpk.n,\r
+        clientPubKey: offer.pubKey\r
+      }))\r
 \r
-      })\r
     }).catch((err) => {\r
       console.log(`error in host answer ${err}`)\r
     })\r
@@ -91,12 +93,13 @@ function handleNewClientConnection(offer) {
     dataChannel = evt.channel\r
     dataChannel.onmessage = (msg) => {\r
       let clientMessage = JSON.parse(msg.data)\r
-      console.log(clientMessage)\r
+      console.log(`client message is ${clientMessage}`)\r
       hpc.setRemoteDescription(clientMessage.sdp).then(() => {\r
         console.log('should be streaming now')\r
       })\r
     }\r
     dataChannel.onopen = () => {\r
+      /* If !screenStream, gUM */\r
       screenStream.getTracks().forEach( (track) => {\r
         hpc.addTrack(track, screenStream)\r
       })\r
@@ -168,6 +171,10 @@ if ("WebSocket" in window) {
       console.log(err);\r
     }); // always check for errors at the end.\r
 \r
+    getPublicKey()\r
+    .then((hpkVal) => {\r
+      hpk = hpkVal\r
+    })\r
     wsock = new WebSocket(`${_strapp_protocol}://${window.location.hostname}:${_strapp_port}`)\r
     wsock.onopen = () => {\r
       console.log(`Strapped to ${_strapp_protocol}://${window.location.hostname}:${_strapp_port}`)\r
@@ -177,6 +184,7 @@ if ("WebSocket" in window) {
       /* msg is either offer or ice candidate or ice candidate request*/\r
 \r
       /* What if data null? */\r
+      console.log(`serverMsg = ${serverMsg.data}`)\r
       let msg = JSON.parse(serverMsg.data)\r
 \r
       const clientID = msg.pubKey\r