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}`)
7 console
.log(`Strapped to ${_strapp_protocol}://${window.location.hostname}:${_strapp_port}`)
10 wsock
.onmessage
= (msg
) => {
11 /* Message is offer from client */
12 /* TODO: Determine which client ?? */
13 console
.log("Incoming connection " + msg
)
15 /* TODO: State machine to parse offer */
17 /* New Client Connection*/
18 hpc
= new RTCPeerConnection()
20 hpc
.createAnswer().then((offer
) => {
21 return hpc
.setLocalDescription(offer
)
23 return hpc
.setRemoteDescription(msg
.sdp
)
25 const hpk
= getPublicKey()
28 sdp
: hpc
.localDescription
,
32 hostsdp
: hpc
.localDescription
,
33 clientsdp
: hpc
.remoteDescription
,
34 clientPubKey
: msg
.pubKey
42 document
.addEventListener('DOMContentLoaded', () => {
43 document
.body
.innerHTML
= 'Websockets not supported in your browser'
46 /* TODO: duplicate in both client.js and host.jhs */
47 function getPublicKey() {
48 /* Check local storage for public key */
49 if (window
.localStorage
.getItem('public-key') === undefined) {
50 /* If doesn't exist, generate public and private key pair, store in
52 crypto
.subtle
.generateKey({name
:'RSA-OAEP', length
: 192}, true, ['encrypt', 'decrypt'])
54 /* TODO: Do we need to store the private key as well? */
55 window
.localStorage
.setItem('public-key', keyPair
.type
.public.toString())
58 console
.log(window
.localStorage
.getItem('public-key'))
59 return window
.localStorage
.getItem('public-key')