X-Git-Url: https://git.kengrimes.com/?p=henge%2Fkiak.git;a=blobdiff_plain;f=client.js;h=8813f550da280b974e2ed144858aca2f12288d7c;hp=5607fa7d194657f8ee2bfbb227ee5a1ebd488672;hb=d33866cf8b1a188ad9ab23a934986bfd1b4726cb;hpb=57d01d593d30b98cbba1d39a2ebb98fcc4e91bcb diff --git a/client.js b/client.js index 5607fa7..8813f55 100644 --- a/client.js +++ b/client.js @@ -4,74 +4,99 @@ document.title = "Strapp.io Client" body.appendChild(root) document.body = body -/*************/ -/* UNTESTED */ -/************ - - /* 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 pollServerTimeout(url, data, resolve, reject) { - console.log('Polling server with offer ' + data) - const request = XMLHttpRequest() - request.open('GET', url) + console.log(`Polling server ${url} with`) + console.log(data) + const request = new XMLHttpRequest() + request.open('GET', url, true) request.setRequestHeader('Content-Type', 'application/json' ) + request.setRequestHeader('X-Strapp-Type', JSON.stringify(data)) request.onreadystatechange = () => { if (request.status === 200) { - console.log('recieved answer from host ' + request.response) + console.log(request.response) resolve(request.response) } else if (request.status === 504) { + console.log('timed out, resending') pollServerTimeout(url, data, resolve, reject) } else { - reject('server errored out with ' + request.status) + reject('server unhandled response of status ' + request.status) } } - request.send(data) + console.log(data) + request.send() } -/* TODO: Possible to pass resolve/reject to functions? */ +/* TODO: All this does is wrap a function in a promise */ function pollServer(url, clientPubKey, func) { return new Promise((resolve, reject) => { func(url, clientPubKey, resolve, reject ) }) } -/* If https connection, should be already defined. If not https,*/ + +/* TODO: duplicate in both client.js and host.js */ function getPublicKey() { - /* Check local storage for public key */ - if (window.localStorage.getItem('public-key') === undefined) { - /* If doesn't exist, generate public and private key pair, store in - local storage */ - crypto.subtle.generateKey({name:'RSA-OAEP', length: 192}, true, ['encrypt', 'decrypt']) - .then((keyPair) => { - /* TODO: Do we need to store the private key as well? */ - window.localStorage.setItem('public-key', keyPair.type.public.toString()) - }) - } - console.log(window.localStorage.getItem('public-key')) - return window.localStorage.getItem('public-key') + return new Promise( (resolve, reject) => { + /* Check local storage for public key */ + if (!window.localStorage.getItem('public-key')) { + console.log('public key is undefined') + /* If doesn't exist, generate public and private key pair, store in + local storage */ + crypto.subtle.generateKey( + { name:'RSA-OAEP', + 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')) + } + }) + } /* Create, set, and get client Offer. Poll server for host answer. Set host answer as client remoteDescription */ const cpc = new RTCPeerConnection() +console.log('creating offer') cpc.createOffer().then((offer) => { - console.log('creating offer which is ' + offer) return cpc.setLocalDescription(offer) -}).then(() => { +}) + .then(() => { console.log('sessionDescriptionInit = ' + cpc.localDescription) - const cpk = getPublicKey() - let offer = { - cmd: '> sdp pubKey' - sdp: cpc.localDescription, - pubKey: cpk - } - /* Poll for answer */ - return pollServer(window.location, offer, pollServerTimeout) -}).then((answer) => { - /* TODO: Extract sdp from answer ?*/ - console.log(answer) - /* State machine to parse answer */ - cpc.setRemoteDescription(answer.sdp) + getPublicKey().then((cpk) => { + console.log('cpk is' + cpk) + let offer = { + cmd: '> sdp pubKey', + sdp: cpc.localDescription, + pubKey: cpk + } + /* Poll for answer */ + return pollServer(window.location, offer, pollServerTimeout) + }).then((answer) => { + //console.log(answer) + /* TODO: State machine to parse answer */ + cpc.setRemoteDescription(answer.sdp) + }).catch( (err) => { + console.log('error in sdp handshake: ' + err) + }) }) + .catch((err) => { + console.log(err) + })