ice candidate addition for host + client now works
[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 const conf = {"iceServers": [{ "urls": "stun:stun.1.google.com:19302" }] }
7 let dataChannel
8
9 /* TODO: duplicate in both client.js and host.js */
10 function getPublicKey() {
11 return new Promise( (resolve, reject) => {
12 /* Check local storage for public key */
13 if (!window.localStorage.getItem('public-key')) {
14 /* If doesn't exist, generate public and private key pair, store in
15 local storage */
16 crypto.subtle.generateKey(
17 { name:'RSA-OAEP',
18 modulusLength: 2048,
19 publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
20 hash: {name: "SHA-256"}
21 },
22 true,
23 ['encrypt', 'decrypt']
24 ).then((keyPair) => {
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'))
30 resolve(exportedKey)
31 })
32
33 })
34 }
35 else {
36 resolve(window.localStorage.getItem('publicKey'))
37 }
38 })
39
40 }
41
42 function postServer(url, data) {
43 const request = new XMLHttpRequest()
44 request.open('POST', url, true)
45 request.setRequestHeader('Content-Type', 'application/json' )
46 request.setRequestHeader('X-Strapp-Type', 'ice-candidate-submission')
47 request.send(data)
48 }
49
50 /* TODO: All this does is wrap a function in a promise. Allows pollServerForAnswer
51 to call itself recursively with the same promise */
52 function pollServer(url, clientPubKey, func) {
53 return new Promise((resolve, reject) => {
54 func(url, clientPubKey, resolve, reject )
55 })
56 }
57
58 /* Poll the server. Send get request, wait for timeout, send another request.
59 Do this until...? Can be used for either reconnecting or waiting for answer*/
60 function pollServerForAnswer(url, data, resolve, reject) {
61 const request = new XMLHttpRequest()
62 request.open('GET', url, true)
63 /* But there is no JSON? */
64 request.setRequestHeader('Content-Type', 'application/json' )
65 request.setRequestHeader('X-Strapp-Type', 'client-sdp-offer')
66 request.setRequestHeader('X-Client-Offer', JSON.stringify(data))
67 request.onreadystatechange = () => {
68 if (request.status === 200) {
69 if(request.readyState === 4) {
70 console.log('Client: Recieved Answer from Host')
71 console.log(request)
72 resolve(request.response)
73 }
74 }
75 else if (request.status === 504) {
76 console.log('timed out, resending')
77 pollServerForAnswer(url, data, resolve, reject)
78 }
79 else {
80 reject('server unhandled response of status ' + request.status)
81 }
82 }
83 request.send()
84 }
85
86 /* Poll server for ice candidates until ice is complete */
87 function pollServerForICECandidate(cpc, url, pubKey) {
88 let intervalID = window.setInterval(() => {
89 if (cpc.iceConnectionState.localeCompare('connected') !== 0
90 && cpc.iceConnectionState.localeCompare('completed') !== 0) {
91 console.log('Client: Polling server begin for intervalID = ' + intervalID)
92 console.log('Client: Requesting ICE Candidates from server')
93 const request = new XMLHttpRequest()
94 request.open('GET', url, true)
95 request.setRequestHeader('Content-Type', 'application/json' )
96 request.setRequestHeader('X-Strapp-Type', 'ice-candidate-request')
97 request.setRequestHeader('X-client-pubkey', pubKey)
98 request.onreadystatechange = () => {
99 if (request.status === 200) {
100 if(request.readyState === 4) {
101 console.log('Client: Recieved ICE response from Host')
102 let response = JSON.parse(request.response)
103 switch(response['iceState']) {
104 case "a":
105 cpc.addIceCandidate(new RTCIceCandidate(response.ice))
106 break
107 case "g": /* Gathering so let interval keep polling */
108 break
109 case "c": /* host iceState == Complete, stop bugging it */
110 clearInterval(intervalID)
111 clearTimeout()
112 break
113 default:
114 console.log('Unhandled iceState in pollServerForICECandidate()' + response['iceState'])
115 break
116 }
117 }
118 }
119 else {
120 console.log('server unhandled response of status ' + request.status)
121 clearInterval(intervalID)
122 }
123 }
124 request.send()
125 }
126 else {
127 clearTimeout()
128 clearInterval(intervalID)
129 }
130 }, 5000)
131 }
132
133 /* Create and send offer -> Send ICE Candidates -> Poll for ICE Candidates */
134 getPublicKey().then((cpk) => {
135 console.log('Client: Create and send offer')
136 const cpc = new RTCPeerConnection(conf)
137
138 cpc.oniceconnectionstatechange = () => {
139 console.log('iceConnectionState = ' + cpc.iceConnectionState)
140 }
141
142 cpc.onnegotiationneeded = () => {
143 console.log('negotiation needed!')
144 cpc.createOffer().then((offer) => {
145 return cpc.setLocalDescription(offer)
146 })
147 .then(() => {
148 console.log('Client: Sending offer to host')
149 let offer = {
150 cmd: '> sdp pubKey',
151 sdp: cpc.localDescription,
152 pubKey: cpk.n
153 }
154 return pollServer(window.location, offer, pollServerForAnswer)
155 }).then((serverResponse) => {
156 const answer = JSON.parse(serverResponse)
157 console.log('Client: Polling for ICE candidates')
158 pollServerForICECandidate(cpc, window.location, cpk.n)
159 cpc.setRemoteDescription(answer.sdp)
160 cpc.onicecandidate = (event) => {
161 if (event.candidate) {
162 console.log('Client: Sending ice candidate to host')
163 postServer(window.location, JSON.stringify({
164 cmd: '> ice pubkey',
165 ice: event.candidate,
166 pubKey: cpk.n
167 }))
168 }
169 else {
170 console.log('Client: No more Ice Candidates to send')
171 }
172 }
173
174
175 }).catch( (err) => {
176 console.log('error in sdp handshake: ' + err)
177 })
178 }
179 /* Start data channel */
180 dataChannel = cpc.createDataChannel("sendChannel");
181 dataChannel.onmessage = (msg) => {
182 console.log(msg.data)
183 }
184 dataChannel.onopen = () => {
185 dataChannel.send(`Hi from the Client`)
186 }
187
188 })