fixed null body for get headers
[henge/kiak.git] / client.js
1 const body = document.createElement('body')
2 const root = document.createElement('div')
3 document.title = "Strapp.io Client"
4 body.appendChild(root)
5 document.body = body
6
7 /* Poll the server. Send get request, wait for timeout, send another request.
8 Do this until...? Can be used for either reconnecting or waiting for answer*/
9 function pollServerTimeout(url, data, resolve, reject) {
10 console.log(`Polling server ${url} with`)
11 console.log(data)
12 const request = new XMLHttpRequest()
13 request.open('GET', url, true)
14 request.setRequestHeader('Content-Type', 'application/json' )
15 request.setRequestHeader('X-Strapp-Type', JSON.stringify(data))
16 request.onreadystatechange = () => {
17 if (request.status === 200) {
18 console.log(request.response)
19 resolve(request.response)
20 }
21 else if (request.status === 504) {
22 console.log('timed out, resending')
23 pollServerTimeout(url, data, resolve, reject)
24 }
25 else {
26 reject('server unhandled response of status ' + request.status)
27 }
28 }
29 console.log(data)
30 request.send()
31 }
32
33 /* TODO: All this does is wrap a function in a promise */
34 function pollServer(url, clientPubKey, func) {
35 return new Promise((resolve, reject) => {
36 func(url, clientPubKey, resolve, reject )
37 })
38 }
39
40 /* TODO: duplicate in both client.js and host.js */
41 function getPublicKey() {
42 return new Promise( (resolve, reject) => {
43 /* Check local storage for public key */
44 if (!window.localStorage.getItem('public-key')) {
45 console.log('public key is undefined')
46 /* If doesn't exist, generate public and private key pair, store in
47 local storage */
48 crypto.subtle.generateKey(
49 { name:'RSA-OAEP',
50 modulusLength: 2048,
51 publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
52 hash: {name: "SHA-256"}
53 },
54 true,
55 ['encrypt', 'decrypt']
56 ).then((keyPair) => {
57 /* TODO: Do we need to store the private key as well? */
58 crypto.subtle.exportKey('jwk', keyPair.publicKey)
59 .then((exportedKey) => {
60 window.localStorage.setItem('publicKey', exportedKey)
61 console.log('public key is' + window.localStorage.getItem('publicKey'))
62 resolve(exportedKey)
63 })
64
65 })
66 }
67 else {
68 resolve(window.localStorage.getItem('publicKey'))
69 }
70 })
71
72 }
73
74 /* Create, set, and get client Offer. Poll server for host answer.
75 Set host answer as client remoteDescription */
76 const cpc = new RTCPeerConnection()
77 console.log('creating offer')
78 cpc.createOffer().then((offer) => {
79 return cpc.setLocalDescription(offer)
80 })
81 .then(() => {
82 console.log('sessionDescriptionInit = ' + cpc.localDescription)
83 getPublicKey().then((cpk) => {
84 console.log('cpk is' + cpk)
85 let offer = {
86 cmd: '> sdp pubKey',
87 sdp: cpc.localDescription,
88 pubKey: cpk
89 }
90 /* Poll for answer */
91 return pollServer(window.location, offer, pollServerTimeout)
92 }).then((answer) => {
93 //console.log(answer)
94 /* TODO: State machine to parse answer */
95 cpc.setRemoteDescription(answer.sdp)
96 }).catch( (err) => {
97 console.log('error in sdp handshake: ' + err)
98 })
99 })
100 .catch((err) => {
101 console.log(err)
102 })