- modulusLength: 2048,
- publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
- hash: {name: "SHA-256"}
- },
- true,
- ['encrypt', 'decrypt']
- ).then((keyPair) => {
- /* TODO: Do we need to store the private key as well? */
- crypto.subtle.exportKey('jwk', keyPair.publicKey)
- .then((exportedKey) => {
- window.localStorage.setItem('publicKey', exportedKey)
- console.log('public key is' + window.localStorage.getItem('publicKey'))
- resolve(exportedKey)
- })
-
- })
- }
- else {
- resolve(window.localStorage.getItem('publicKey'))
+ modulusLength: 2048,
+ publicExponent: new Uint8Array([0x01, 0x00, 0x01]),
+ hash: {name: "SHA-256"}
+ },
+ true,
+ ['encrypt', 'decrypt']
+ ).then((keyPair) => {
+ /* TODO: Do we need to store the private key as well? */
+ crypto.subtle.exportKey('jwk', keyPair.publicKey)
+ .then((exportedKey) => {
+ window.localStorage.setItem('publicKey', exportedKey)
+ console.log('public key is' + window.localStorage.getItem('publicKey'))
+ resolve(exportedKey)
+ })
+
+ })
+ }
+ else {
+ resolve(window.localStorage.getItem('publicKey'))
+ }
+})
+
+}
+
+function sendHost(url, data) {
+ const request = new XMLHttpRequest()
+ request.open('GET', url, true)
+ request.setRequestHeader('Content-Type', 'application/json' )
+ request.setRequestHeader('x-strapp-type', 'ice-candidate-submission')
+ request.send(data)
+}
+
+/* Poll the server. Send get request, wait for timeout, send another request.
+Do this until...? Can be used for either reconnecting or waiting for answer*/
+function requestHostAnswer(url, data) {
+ return new Promise((resolve, reject) => {
+ const request = new XMLHttpRequest()
+ request.open('GET', url, true)
+ /* But there is no JSON? */
+ request.setRequestHeader('Content-Type', 'application/json' )
+ request.setRequestHeader('x-strapp-type', 'client-sdp-offer')
+ request.setRequestHeader('x-strapp-pubkey', data.pubKey)
+ request.onreadystatechange = () => {
+ if (request.status === 200) {
+ if(request.readyState === 4) {
+ console.log('Client: Recieved Answer from Host')
+ console.log(request)
+ resolve(request.response)
+ }
+ }
+ else if (request.status === 504) {
+ console.log('timed out, resending')
+ resolve(requestHostAnswer(url, data))
+ }
+ else {
+ reject('server unhandled response of status ' + request.status)
+ }