ice candidate addition for host + client now works
[henge/kiak.git] / host.js
diff --git a/host.js b/host.js
index 9b84f5c..01c56a2 100644 (file)
--- a/host.js
+++ b/host.js
@@ -52,16 +52,18 @@ function handleNewClientConnection(offer) {
       getPublicKey().then((hpk) => {
         hpc.onicecandidate = (event) => {
           if (event.candidate) {
+            console.log('Host: Allocating ice candidate for client')
             iceCandidates.push(JSON.stringify({
-              cmd: '< ice pubKey',
+              cmd: "< ice pubKey",
               ice: event.candidate,
               hostPubKey: hpk.n, /* TODO: do we need to send this? */
               clientPubKey: offer.pubKey,
-              iceCandidateAvailable: true
+              iceState: "a"
             }))
           }
           else {
             console.log('Host: Finished sending ICE candidates')
+            console.log(hpc)
           }
         }
         console.log('Host: Sending answer to Client')
@@ -77,7 +79,9 @@ function handleNewClientConnection(offer) {
           dataChannel.onmessage = (msg) => {
             console.log(msg.data)
           }
-          dataChannel.send('Hi from the host')
+          dataChannel.onopen = () => {
+            dataChannel.send(`Hi ${offer.pubKey} -host`)
+          }
         }
         hpc.oniceconnectionstatechange = () => {
           console.log('iceConnectionState = ' + hpc.iceConnectionState)
@@ -88,8 +92,6 @@ function handleNewClientConnection(offer) {
     })
   })
 
-
-
 }
 
 function handleNewIceSubmission(msg) {
@@ -100,13 +102,20 @@ function handleNewIceSubmission(msg) {
 }
 
 function handleIceRequest(msg) {
-  console.log('Host: Sending ice candidate to client')
-  const hpc = clients.get(msg)
-  const iceCandidates = iceCandidates.pop()
+  console.log('Host: Handling ice candidate request')
+  console.log(iceCandidates)
+  const hpc = clients.get(msg.pubKey)
+  const iceCandidate = iceCandidates.pop()
   if (iceCandidate !== undefined) {
     wsock.send(iceCandidate)
   } else {
-    wsock.send('no ice candidates')
+    if (hpc.iceGatheringState.localeCompare('gathering') === 0) {
+      wsock.send(`{"cmd" : "< ice pubKey", "clientPubKey":"${msg.pubKey}", "iceState": "g"}`)
+    }
+    else if (hpc.iceGatheringState.localeCompare('complete') === 0) {
+      wsock.send(`{"cmd" : "< ice pubKey", "clientPubKey":"${msg.pubKey}", "iceState": "c"}`)
+    }
+
   }
 
 }
@@ -120,10 +129,10 @@ if ("WebSocket" in window) {
     wsock.onmessage = (serverMsg) => {
       /* msg is either offer or ice candidate or ice candidate request*/
 
-
+      /* What if data null? */
       let msg = JSON.parse(serverMsg.data)
 
-      const clientID = msg.pubKey || msg
+      const clientID = msg.pubKey
 
       /* TODO: redo this trash */
       if (clients.has(clientID)) {