1 document
.title
= "Strapp.io Host"
2 const conf
= {"iceServers": [{ "urls": "stun:stun.1.google.com:19302" }] }
3 const clients
= new Map([])
4 const iceCandidates
= []
8 /* TODO: duplicate in both client.js and host.jhs */
9 function getPublicKey() {
10 return new Promise( (resolve
, reject
) => {
11 /* Check local storage for public key */
12 if (!window
.localStorage
.getItem('public-key')) {
13 console
.log('public key is undefined')
14 /* If doesn't exist, generate public and private key pair, store in
16 crypto
.subtle
.generateKey(
19 publicExponent
: new Uint8Array([0x01, 0x00, 0x01]),
20 hash
: {name
: "SHA-256"}
23 ['encrypt', 'decrypt']
25 /* TODO: Do we need to store the private key as well? */
26 crypto
.subtle
.exportKey('jwk', keyPair
.publicKey
)
27 .then((exportedKey
) => {
28 window
.localStorage
.setItem('publicKey', exportedKey
)
29 console
.log('public key is' + window
.localStorage
.getItem('publicKey'))
36 resolve(window
.localStorage
.getItem('publicKey'))
41 function handleNewClientConnection(offer
) {
42 /* New Client Connection*/
43 hpc
= new RTCPeerConnection(conf
)
45 clients
.set(offer
.pubKey
, hpc
)
46 hpc
.setRemoteDescription(offer
.sdp
)
48 hpc
.createAnswer().then((answer
) => {
49 return hpc
.setLocalDescription(answer
)
52 getPublicKey().then((hpk
) => {
53 hpc
.onicecandidate
= (event
) => {
54 if (event
.candidate
) {
55 console
.log('Host: Allocating ice candidate for client')
56 iceCandidates
.push(JSON
.stringify({
59 hostPubKey
: hpk
.n
, /* TODO: do we need to send this? */
60 clientPubKey
: offer
.pubKey
,
61 iceCandidateAvailable
: true
65 console
.log('Host: Finished sending ICE candidates')
68 console
.log('Host: Sending answer to Client')
69 wsock
.send(JSON
.stringify({
71 sdp
: hpc
.localDescription
,
73 clientPubKey
: offer
.pubKey
75 hpc
.ondatachannel
= (evt
) => {
76 dataChannel
= evt
.channel
78 dataChannel
.onmessage
= (msg
) => {
81 dataChannel
.onopen
= () => {
82 dataChannel
.send(`Hi ${offer.pubKey} -host`)
85 hpc
.oniceconnectionstatechange
= () => {
86 console
.log('iceConnectionState = ' + hpc
.iceConnectionState
)
90 console
.log(`error in host answer ${err}`)
98 function handleNewIceSubmission(msg
) {
99 console
.log('Host: Adding new ice candidate')
100 const hpc
= clients
.get(msg
.pubKey
)
101 let candidate
= new RTCIceCandidate(msg
.ice
)
102 hpc
.addIceCandidate(candidate
)
105 function handleIceRequest(msg
) {
106 console
.log('Host: Handling ice candidate request')
107 console
.log(iceCandidates
)
108 const hpc
= clients
.get(msg
)
109 const iceCandidate
= iceCandidates
.pop()
110 if (iceCandidate
!== undefined) {
111 wsock
.send(iceCandidate
)
113 wsock
.send(`{"cmd" : "< ice pubKey", "clientPubKey":"${msg.pubKey}", "iceCandidateAvailable": false}`)
117 if ("WebSocket" in window
) {
118 document
.addEventListener('DOMContentLoaded', (event
) => {
119 wsock
= new WebSocket(`${_strapp_protocol}://${window.location.hostname}:${_strapp_port}`)
120 wsock
.onopen
= () => {
121 console
.log(`Strapped to ${_strapp_protocol}://${window.location.hostname}:${_strapp_port}`)
124 wsock
.onmessage
= (serverMsg
) => {
125 /* msg is either offer or ice candidate or ice candidate request*/
127 /* What if data null? */
128 let msg
= JSON
.parse(serverMsg
.data
)
130 const clientID
= msg
.pubKey
132 /* TODO: redo this trash */
133 if (clients
.has(clientID
)) {
135 handleNewIceSubmission(msg
)
136 } else if (msg
.sdp
) {
137 //handleRepeatedOffer
139 handleIceRequest(msg
)
144 console
.log('Host: Client that doesnt exist is sending ice submissions')
145 } else if (msg
.sdp
) {
146 handleNewClientConnection(msg
)
148 console
.log('Host: Client that doesnt exist is sending ice requests')
155 document
.addEventListener('DOMContentLoaded', () => {
156 document
.body
.innerHTML
= 'Websockets not supported in your browser'