client done
authorjordan lavatai <jordanlavatai@gmail.com>
Fri, 30 Jun 2017 03:37:23 +0000 (20:37 -0700)
committerjordan lavatai <jordanlavatai@gmail.com>
Fri, 30 Jun 2017 03:37:23 +0000 (20:37 -0700)
README.md
client.js
skel.html

index 0dca832..3952f9b 100644 (file)
--- a/README.md
+++ b/README.md
@@ -7,6 +7,8 @@ TBD
 https://tutorialzine.com/2014/06/10-tips-for-writing-javascript-without-jquery
 https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API
 https://github.com/webrtc/adapter/
+https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API/Connectivity
+https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API/Signaling_and_video_calling
 
 ## License
 
index 3f0aeec..85c139f 100644 (file)
--- a/client.js
+++ b/client.js
@@ -3,3 +3,70 @@ const root = document.createElement('div')
 document.title = "Strapp.io Client"
 body.appendChild(root)
 document.body = body
+
+/* Poll the server. Send get request, wait for timeout, send another request.
+   Do this until...? */
+function pollServerTimeout(url, data, resolve, reject) {
+  console.log('Polling server with offer ' + data)
+  const request = XMLHttpRequest()
+  request.open('GET', url)
+  request.setRequestHeader('Content-Type', 'application/json' )
+  request.onreadystatechange = () => {
+    if (request.status === 200) {
+      console.log('recieved answer from host ' + request.response)
+      resolve(request.response)
+    }
+    else if (request.status === 504) {
+      pollServerTimeout(url, resolve, reject)
+    }
+    else {
+      reject('server errored out with ' + request.status)
+    }
+  }
+  request.send(data)
+}
+
+/* TODO: Possible to pass resolve/reject to functions? */
+function pollServer(url, clientPubKey, func) {
+  return new Promise((resolve, reject) => {
+    func(url, clientPubKey, resolve, reject )
+  })
+}
+/* If https connection, should be already defined. If not https,*/
+function getPublicKey() {
+  /* Check local storage for public key */
+  if (window.localStorage.getItem('public-key') === undefined) {
+    /* If doesn't exist, generate public and private key pair, store in
+    local storage */
+    crypto.subtle.generateKey({name:'RSA-OAEP', length: 192}, true, ['encrypt', 'decrypt'])
+      .then((keyPair) => {
+        /* TODO: Do we need to store the private key as well? */
+        window.localStorage.setItem('public-key', keyPair.type.public.toString())
+      })
+  }
+    console.log(window.localStorage.getItem('public-key'))
+    return window.localStorage.getItem('public-key')
+}
+
+/* Create, set, and get client Offer. Poll server for host answer.
+   Set host answer as client remoteDescription */
+const cpc = new RTCPeerConnection()
+cpc.createOffer().then((offer) => {
+  console.log('creating offer which is ' + offer)
+  return cpc.setLocalDescription(offer)
+}).then(() => {
+  console.log('sessionDescriptionInit = ' + cpc.localDescription)
+  const cpk = getPublicKey()
+  let offer = {
+    cmd: '> sdp pubKey'
+    sdp: cpc.localDescription,
+    pubKey: cpk
+  }
+  /* Poll for answer */
+  return pollServer(window.location, offer, pollServerTimeout)
+}).then((answer) => {
+  /* TODO: Extract sdp from answer ?*/
+  console.log(answer)
+  /* State machine to parse answer */
+  cpc.setRemoteDescription(answer.sdp)
+})
index 0bf45ae..69198c3 100644 (file)
--- a/skel.html
+++ b/skel.html
@@ -1,12 +1,13 @@
 <!DOCTYPE html>
-<html>
+<html manigest="strapp.appcache">
   <head>
     <meta charset="utf-8" />
     <title>Strapp.io</title>
     <!--STRAPP_FAVICO-->
-    <link rel=icon href="./www/favicon-96x96.png">    
+    <link rel=icon href="./www/favicon-96x96.png">
     <!--STRAPP_MANIFEST-->
     <link rel=manifest href="./www/manifest.json">
+    <script src="https://github.com/PeculiarVentures/webcrypto-liner/blob/master/dist/webcrypto-liner.shim.js"></script>
     <script type="text/javascript">
       <!--STRAPP_SRC-->
     </script>