From c6d9ad94b6c74931408d8fd994319924ebf5d4d5 Mon Sep 17 00:00:00 2001 From: jordan lavatai Date: Thu, 29 Jun 2017 20:37:23 -0700 Subject: [PATCH] client done --- README.md | 2 ++ client.js | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ skel.html | 5 +++-- 3 files changed, 72 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0dca832..3952f9b 100644 --- a/README.md +++ b/README.md @@ -7,6 +7,8 @@ TBD https://tutorialzine.com/2014/06/10-tips-for-writing-javascript-without-jquery https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API https://github.com/webrtc/adapter/ +https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API/Connectivity +https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API/Signaling_and_video_calling ## License diff --git a/client.js b/client.js index 3f0aeec..85c139f 100644 --- a/client.js +++ b/client.js @@ -3,3 +3,70 @@ const root = document.createElement('div') document.title = "Strapp.io Client" body.appendChild(root) document.body = body + +/* Poll the server. Send get request, wait for timeout, send another request. + Do this until...? */ +function pollServerTimeout(url, data, resolve, reject) { + console.log('Polling server with offer ' + data) + const request = XMLHttpRequest() + request.open('GET', url) + request.setRequestHeader('Content-Type', 'application/json' ) + request.onreadystatechange = () => { + if (request.status === 200) { + console.log('recieved answer from host ' + request.response) + resolve(request.response) + } + else if (request.status === 504) { + pollServerTimeout(url, resolve, reject) + } + else { + reject('server errored out with ' + request.status) + } + } + request.send(data) +} + +/* TODO: Possible to pass resolve/reject to functions? */ +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,*/ +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') +} + +/* Create, set, and get client Offer. Poll server for host answer. + Set host answer as client remoteDescription */ +const cpc = new RTCPeerConnection() +cpc.createOffer().then((offer) => { + console.log('creating offer which is ' + offer) + return cpc.setLocalDescription(offer) +}).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) +}) diff --git a/skel.html b/skel.html index 0bf45ae..69198c3 100644 --- a/skel.html +++ b/skel.html @@ -1,12 +1,13 @@ - + Strapp.io - + + -- 2.18.0