need to do ICE step
[henge/kiak.git] / client.js
index 8989989..d389945 100644 (file)
--- a/client.js
+++ b/client.js
@@ -3,19 +3,22 @@ const root = document.createElement('div')
 document.title = "Strapp.io Client"
 body.appendChild(root)
 document.body = body
-
+const conf = {"iceServers": [{ "url": "stun:stun.1.google.com:19302" }] }
 /* 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*/
 function pollServerTimeout(url, data, resolve, reject) {
-  console.log('Polling server with offer ' + data)
+  console.log(`Polling server ${url} with ${data}`)
   const request = new XMLHttpRequest()
-  request.open('GET', url)
+  request.open('GET', url, true)
   request.setRequestHeader('Content-Type', 'application/json' )
-  request.setRequestHeader('X-Strapp-Type', 'o' )
+  request.setRequestHeader('X-Strapp-Type', JSON.stringify(data))
   request.onreadystatechange = () => {
     if (request.status === 200) {
-      console.log(request.response)
-      resolve(request.response)
+      if(request.readyState === 4) {
+        console.log('Client: Recieved answer from Host')
+        console.log(request)
+        resolve(request.response)
+      }
     }
     else if (request.status === 504) {
       console.log('timed out, resending')
@@ -25,8 +28,7 @@ function pollServerTimeout(url, data, resolve, reject) {
       reject('server unhandled response of status ' + request.status)
     }
   }
-  console.log(data)
-  request.send('data in stufff and stuff in data')
+  request.send()
 }
 
 /* TODO: All this does is wrap a function in a promise */
@@ -72,11 +74,14 @@ function getPublicKey() {
 
 /* Create, set, and get client Offer. Poll server for host answer.
    Set host answer as client remoteDescription */
-const cpc = new RTCPeerConnection()
-console.log('creating offer')
+const cpc = new RTCPeerConnection(conf)
+cpc.oniceconnectionstatechange = () => {
+  console.log('iceConnectionState = ' + cpc.iceConnectionState)
+}
 cpc.createOffer().then((offer) => {
   return cpc.setLocalDescription(offer)
-}).then(() => {
+})
+  .then(() => {
   console.log('sessionDescriptionInit = ' + cpc.localDescription)
   getPublicKey().then((cpk) => {
     console.log('cpk is' + cpk)
@@ -85,13 +90,35 @@ cpc.createOffer().then((offer) => {
       sdp: cpc.localDescription,
       pubKey: cpk
     }
+    cpc.onicecandidate = (event) => {
+      if (event.candidate) {
+        console.log('Client: Sending ice candidate to host')
+        pollServer(window.location, wsock.send(JSON.stringify({
+          cmd: '> ice pubkey',
+          ice: event.candidate,
+          pubKey: cpk /* TODO: do we need to send this? */
+        })), pollServerTimeout)
+      }
+      else {
+        /* Set up data channel here */
+        console.log('Client: Finished setting up ICE candidates')
+      }
+    }
+    /* TODO: start polling for ice candidates, and then addIceCandidate() to cpc */
+    //pollServer(window.location, {ice}, pollServerTimeout)
+
+
     /* Poll for answer */
     return pollServer(window.location, offer, pollServerTimeout)
-  }).then((answer) => {
-    //console.log(answer)
+  }).then((serverResponse) => {
+    const answer = JSON.parse(serverResponse)
+    console.log(answer)
     /* TODO: State machine to parse answer */
+    console.log('Setting Remote Description')
     cpc.setRemoteDescription(answer.sdp)
   }).catch( (err) => {
     console.log('error in sdp handshake: ' + err)
   })
+}).catch((err) => {
+    console.log(err)
 })