added host
[henge/kiak.git] / host.js
diff --git a/host.js b/host.js
index 939c8f2..e6600de 100644 (file)
--- a/host.js
+++ b/host.js
@@ -1,17 +1,42 @@
-const body = document.createElement('body')
-const root = document.createElement('div')
 document.title = "Strapp.io Host"
+const clients = [] //TODO: Change to Map
 if ("WebSocket" in window) {
-  const wsock = new WebSocket("wss://" + window.location.host + ":" +  _strapp_port)
-  wsock.onopen = () => {
-    console.log("Strapped to wss://" + window.location.host + ":" +  _strapp_port)
-  }
-  wsock.onmessage = (evt) => {
-    console.log("Incoming connection from " + evt.data)
-    console.log("TODO: Open a socket to this client")
-  }
-} else {
-  root.appendChild(document.createTextNode("Your browser does not support Strapp"))
+  document.addEventListener('DOMContentLoaded', (event) => {
+    const wsock = new WebSocket(`${_strapp_protocol}://${window.location.hostname}:${_strapp_port}`)
+    wsock.onopen = () => {
+      console.log(`Strapped to ${_strapp_protocol}://${window.location.hostname}:${_strapp_port}`)
+
+    }
+    wsock.onmessage = (msg) => {
+      /* Message is offer from client */
+      console.log("Incoming connection " + msg)
+
+      /* State machine to parse offer */
+
+      /* New Client Connection*/
+      hpc = new RTCPeerConnection()
+
+      hpc.createAnswer().then((offer) => {
+        return hpc.setLocalDescription(offer)
+      }).then(() => {
+        return hpc.setRemoteDescription(msg.sdp)
+      }).then(() => {
+        const hpk = getPublicKey()
+        wsock.send({
+          cmd: '< sdp pubKey'
+          sdp: hpc.localDescription
+          pubKey: hpk
+        })
+        clients.push({
+          sdp: hpc.localDescription
+          clientPubKey: msg.pubKey
+        })
+      })
+    }
+  })
+}
+else {
+  document.addEventListener('DOMContentLoaded', () => {
+    document.body.innerHTML = 'Websockets not supported in your browser'
+  })
 }
-body.appendChild(root)
-document.body = body