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