ice candidate addition for host + client now works
[henge/kiak.git] / client.js
index 956971a..2aa4950 100644 (file)
--- a/client.js
+++ b/client.js
@@ -15,27 +15,27 @@ function getPublicKey() {
       local storage */
       crypto.subtle.generateKey(
         { name:'RSA-OAEP',
-          modulusLength: 2048,
-          publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
-          hash: {name: "SHA-256"}
-        },
-        true,
-        ['encrypt', 'decrypt']
-        ).then((keyPair) => {
-          /* TODO: Do we need to store the private key as well? */
-          crypto.subtle.exportKey('jwk', keyPair.publicKey)
-          .then((exportedKey) => {
-            window.localStorage.setItem('publicKey', exportedKey)
-            console.log('public key is' + window.localStorage.getItem('publicKey'))
-            resolve(exportedKey)
-          })
+        modulusLength: 2048,
+        publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
+        hash: {name: "SHA-256"}
+      },
+      true,
+      ['encrypt', 'decrypt']
+    ).then((keyPair) => {
+      /* TODO: Do we need to store the private key as well? */
+      crypto.subtle.exportKey('jwk', keyPair.publicKey)
+      .then((exportedKey) => {
+        window.localStorage.setItem('publicKey', exportedKey)
+        console.log('public key is' + window.localStorage.getItem('publicKey'))
+        resolve(exportedKey)
+      })
 
-        })
-    }
-    else {
-      resolve(window.localStorage.getItem('publicKey'))
-    }
-  })
+    })
+  }
+  else {
+    resolve(window.localStorage.getItem('publicKey'))
+  }
+})
 
 }
 
@@ -48,7 +48,7 @@ function postServer(url, data) {
 }
 
 /* TODO: All this does is wrap a function in a promise. Allows pollServerForAnswer
-         to call itself recursively with the same promise */
+to call itself recursively with the same promise */
 function pollServer(url, clientPubKey, func) {
   return new Promise((resolve, reject) => {
     func(url, clientPubKey, resolve, reject )
@@ -56,7 +56,7 @@ function pollServer(url, clientPubKey, func) {
 }
 
 /* Poll the server. Send get request, wait for timeout, send another request.
-   Do this until...? Can be used for either reconnecting or waiting for answer*/
+Do this until...? Can be used for either reconnecting or waiting for answer*/
 function pollServerForAnswer(url, data, resolve, reject) {
   const request = new XMLHttpRequest()
   request.open('GET', url, true)
@@ -87,30 +87,41 @@ function pollServerForAnswer(url, data, resolve, reject) {
 function pollServerForICECandidate(cpc, url, pubKey) {
   let intervalID = window.setInterval(() => {
     if (cpc.iceConnectionState.localeCompare('connected') !== 0
-      && cpc.iceConnectionState.localeCompare('completed') !== 0) {
-        console.log('Client: Polling server begin for intervalID = ' + intervalID)
-         console.log('Client: Requesting ICE Candidates from server')
-         const request = new XMLHttpRequest()
-         request.open('GET', url, true)
-         request.setRequestHeader('Content-Type', 'application/json' )
-         request.setRequestHeader('X-Strapp-Type', 'ice-candidate-request')
-         request.setRequestHeader('X-client-pubkey', pubKey)
-         request.onreadystatechange = () => {
-           if (request.status === 200) {
-             if(request.readyState === 4) {
-               console.log('Client: Recieved ICE Candidate from Host')
-               cpc.addIceCandidate(new RTCIceCandidate(JSON.parse(request.response).ice))
-             }
-           }
-           else if (request.status === 204) {
-             console.log('Ice Candidate unavailable, trying again in one second')
-           }
-           else {
-             console.log('server unhandled response of status ' + request.status)
-             clearInterval(intervalID)
-           }
-         }
-         request.send()
+    && cpc.iceConnectionState.localeCompare('completed') !== 0) {
+      console.log('Client: Polling server begin for intervalID = ' + intervalID)
+      console.log('Client: Requesting ICE Candidates from server')
+      const request = new XMLHttpRequest()
+      request.open('GET', url, true)
+      request.setRequestHeader('Content-Type', 'application/json' )
+      request.setRequestHeader('X-Strapp-Type', 'ice-candidate-request')
+      request.setRequestHeader('X-client-pubkey', pubKey)
+      request.onreadystatechange = () => {
+        if (request.status === 200) {
+          if(request.readyState === 4) {
+            console.log('Client: Recieved ICE response from Host')
+            let response = JSON.parse(request.response)
+            switch(response['iceState']) {
+              case "a":
+                cpc.addIceCandidate(new RTCIceCandidate(response.ice))
+                break
+              case "g": /* Gathering so let interval keep polling */
+                break
+              case "c": /* host iceState == Complete, stop bugging it */
+                clearInterval(intervalID)
+                clearTimeout()
+                break
+              default:
+                console.log('Unhandled iceState in pollServerForICECandidate()' + response['iceState'])
+                break
+            }
+          }
+        }
+        else {
+          console.log('server unhandled response of status ' + request.status)
+          clearInterval(intervalID)
+        }
+      }
+      request.send()
     }
     else {
       clearTimeout()