-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")
+ 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 */
+ /* TODO: Determine which client ?? */
+ console.log("Incoming connection " + msg)
+
+ /* TODO: 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({
+ hostsdp: hpc.localDescription,
+ clientsdp: hpc.remoteDescription,
+ clientPubKey: msg.pubKey
+ })
+
+ })
+ }
+ })
+}
+else {
+ document.addEventListener('DOMContentLoaded', () => {
+ document.body.innerHTML = 'Websockets not supported in your browser'
+ })
+}
+/* TODO: duplicate in both client.js and host.jhs */
+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())
+ })
}
-} else {
- root.appendChild(document.createTextNode("Your browser does not support Strapp"))
+ console.log(window.localStorage.getItem('public-key'))
+ return window.localStorage.getItem('public-key')
}
-body.appendChild(root)
-document.body = body