added host
[henge/kiak.git] / host.js
1 document.title = "Strapp.io Host"
2 const clients = [] //TODO: Change to Map
3 if ("WebSocket" in window) {
4 document.addEventListener('DOMContentLoaded', (event) => {
5 const wsock = new WebSocket(`${_strapp_protocol}://${window.location.hostname}:${_strapp_port}`)
6 wsock.onopen = () => {
7 console.log(`Strapped to ${_strapp_protocol}://${window.location.hostname}:${_strapp_port}`)
8
9 }
10 wsock.onmessage = (msg) => {
11 /* Message is offer from client */
12 console.log("Incoming connection " + msg)
13
14 /* State machine to parse offer */
15
16 /* New Client Connection*/
17 hpc = new RTCPeerConnection()
18
19 hpc.createAnswer().then((offer) => {
20 return hpc.setLocalDescription(offer)
21 }).then(() => {
22 return hpc.setRemoteDescription(msg.sdp)
23 }).then(() => {
24 const hpk = getPublicKey()
25 wsock.send({
26 cmd: '< sdp pubKey'
27 sdp: hpc.localDescription
28 pubKey: hpk
29 })
30 clients.push({
31 sdp: hpc.localDescription
32 clientPubKey: msg.pubKey
33 })
34 })
35 }
36 })
37 }
38 else {
39 document.addEventListener('DOMContentLoaded', () => {
40 document.body.innerHTML = 'Websockets not supported in your browser'
41 })
42 }