X-Git-Url: https://www.kengrimes.com/gitweb/?p=henge%2Fkiak.git;a=blobdiff_plain;f=host.js;h=e6600de00e2c2564bf939d3b6310270156c75657;hp=45ea63ff7f620ffc8fa0f1e7000a0af1746baa62;hb=8a767de924e57ea87bc2d0d0b9cf9864e523d916;hpb=c6d9ad94b6c74931408d8fd994319924ebf5d4d5 diff --git a/host.js b/host.js index 45ea63f..e6600de 100644 --- a/host.js +++ b/host.js @@ -1,19 +1,39 @@ document.title = "Strapp.io Host" -const clients = [] +const clients = [] //TODO: Change to Map if ("WebSocket" in window) { - 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 = (evt) => { - console.log("Incoming connection from " + evt.data) - console.log("TODO: Open a socket to this client") - wsock.send("Got " + evt.data) - clients.push({ - ip: evt.data, - dataChannel: undefined - }) - } + 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', () => {