-document.title = "Strapp.io Host"
-const conf = {"iceServers": [{ "urls": "stun:stun.1.google.com:19302" }] }
-const clients = new Map([])
-const iceCandidates = []
-let dataChannel
-
-
-/* TODO: duplicate in both client.js and host.jhs */
-function getPublicKey() {
- 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'))
- }
- })
-}
-
-function handleNewClientConnection(offer) {
- /* New Client Connection*/
- hpc = new RTCPeerConnection(conf)
- //console.log(offer)
- clients.set(offer.pubKey, hpc)
- hpc.setRemoteDescription(offer.sdp)
- .then(() => {
- hpc.createAnswer().then((answer) => {
- return hpc.setLocalDescription(answer)
- })
- .then(() => {
- getPublicKey().then((hpk) => {
- hpc.onicecandidate = (event) => {
- if (event.candidate) {
- console.log('Host: Allocating ice candidate for client')
- iceCandidates.push(JSON.stringify({
- cmd: "< ice pubKey",
- ice: event.candidate,
- hostPubKey: hpk.n, /* TODO: do we need to send this? */
- clientPubKey: offer.pubKey,
- iceCandidateAvailable: true
- }))
- }
- else {
- console.log('Host: Finished sending ICE candidates')
- }
- }
- console.log('Host: Sending answer to Client')
- wsock.send(JSON.stringify({
- cmd: '< sdp pubKey',
- sdp: hpc.localDescription,
- hostPubKey: hpk.n,
- clientPubKey: offer.pubKey
- }))
- hpc.ondatachannel = (evt) => {
- dataChannel = evt.channel
- console.log(evt)
- dataChannel.onmessage = (msg) => {
- console.log(msg.data)
- }
- dataChannel.onopen = () => {
- dataChannel.send(`Hi ${offer.pubKey} -host`)
- }
- }
- hpc.oniceconnectionstatechange = () => {
- console.log('iceConnectionState = ' + hpc.iceConnectionState)
- }
- })
- }).catch((err) => {
- console.log(`error in host answer ${err}`)
- })
- })
-
-
-
-}
-
-function handleNewIceSubmission(msg) {
- console.log('Host: Adding new ice candidate')
- const hpc = clients.get(msg.pubKey)
- let candidate = new RTCIceCandidate(msg.ice)
- hpc.addIceCandidate(candidate)
-}
-
-function handleIceRequest(msg) {
- console.log('Host: Handling ice candidate request')
- console.log(iceCandidates)
- const hpc = clients.get(msg)
- const iceCandidate = iceCandidates.pop()
- if (iceCandidate !== undefined) {
- wsock.send(iceCandidate)
- } else {
- wsock.send(`{"cmd" : "< ice pubKey", "clientPubKey":"${msg.pubKey}", "iceCandidateAvailable": false}`)
- }
-
-}
-if ("WebSocket" in window) {
- document.addEventListener('DOMContentLoaded', (event) => {
- wsock = new WebSocket(`${_strapp_protocol}://${window.location.hostname}:${_strapp_port}`)
- wsock.onopen = () => {
- console.log(`Strapped to ${_strapp_protocol}://${window.location.hostname}:${_strapp_port}`)
- }
-
- wsock.onmessage = (serverMsg) => {
- /* msg is either offer or ice candidate or ice candidate request*/
-
- /* What if data null? */
- let msg = JSON.parse(serverMsg.data)
-
- const clientID = msg.pubKey
-
- /* TODO: redo this trash */
- if (clients.has(clientID)) {
- if (msg.ice) {
- handleNewIceSubmission(msg)
- } else if (msg.sdp) {
- //handleRepeatedOffer
- } else {
- handleIceRequest(msg)
- }
- }
- else {
- if (msg.ice) {
- console.log('Host: Client that doesnt exist is sending ice submissions')
- } else if (msg.sdp) {
- handleNewClientConnection(msg)
- } else {
- console.log('Host: Client that doesnt exist is sending ice requests')
- }
- }
- }
- })
-}
-else {
- document.addEventListener('DOMContentLoaded', () => {
- document.body.innerHTML = 'Websockets not supported in your browser'
- })
-}
+document.title = "Strapp.io Host"\r
+\r
+\r
+const conf = {"iceServers": [{ "urls": "stun:stun.1.google.com:19302" }] }\r
+const clients = new Map([])\r
+const iceCandidates = []\r
+let dataChannel\r
+let screenStream /* TODO: Remove if can access localStreams */\r
+let tracks = []\r
+\r
+\r
+/* TODO: duplicate in both client.js and host.jhs */\r
+function getPublicKey() {\r
+ return new Promise( (resolve, reject) => {\r
+ /* Check local storage for public key */\r
+ if (!window.localStorage.getItem('public-key')) {\r
+ console.log('public key is undefined')\r
+ /* If doesn't exist, generate public and private key pair, store in\r
+ local storage */\r
+ crypto.subtle.generateKey(\r
+ { name:'RSA-OAEP',\r
+ modulusLength: 2048,\r
+ publicExponent: new Uint8Array([0x01, 0x00, 0x01]),\r
+ hash: {name: "SHA-256"}\r
+ },\r
+ true,\r
+ ['encrypt', 'decrypt']\r
+ ).then((keyPair) => {\r
+ /* TODO: Do we need to store the private key as well? */\r
+ crypto.subtle.exportKey('jwk', keyPair.publicKey)\r
+ .then((exportedKey) => {\r
+ window.localStorage.setItem('publicKey', exportedKey)\r
+ console.log('public key is' + window.localStorage.getItem('publicKey'))\r
+ resolve(exportedKey)\r
+ })\r
+\r
+ })\r
+ }\r
+ else {\r
+ resolve(window.localStorage.getItem('publicKey'))\r
+ }\r
+ })\r
+}\r
+\r
+function handleNewClientConnection(offer) {\r
+ /* New Client Connection*/\r
+ hpc = new RTCPeerConnection(conf)\r
+ //console.log(offer)\r
+ clients.set(offer.pubKey, hpc)\r
+ hpc.setRemoteDescription(offer.sdp)\r
+ .then(() => {\r
+ hpc.createAnswer().then((answer) => {\r
+ return hpc.setLocalDescription(answer)\r
+ })\r
+ .then(() => {\r
+ getPublicKey().then((hpk) => {\r
+ hpc['hpk'] = hpk\r
+ hpc.onicecandidate = (event) => {\r
+ if (event.candidate) {\r
+ console.log('Host: Allocating ice candidate for client')\r
+ iceCandidates.push(JSON.stringify({\r
+ cmd: "< ice pubKey",\r
+ ice: event.candidate,\r
+ hostPubKey: hpk.n, /* TODO: do we need to send this? */\r
+ clientPubKey: offer.pubKey,\r
+ iceState: "a"\r
+ }))\r
+ }\r
+ else {\r
+ console.log('Host: Finished sending ICE candidates')\r
+ }\r
+ }\r
+ console.log('Host: Sending answer to Client ')\r
+ wsock.send(JSON.stringify({\r
+ cmd: '< sdp pubKey',\r
+ sdp: hpc.localDescription,\r
+ hostPubKey: hpk.n,\r
+ clientPubKey: offer.pubKey\r
+ }))\r
+\r
+\r
+ })\r
+ }).catch((err) => {\r
+ console.log(`error in host answer ${err}`)\r
+ })\r
+ })\r
+ hpc.oniceconnectionstatechange = () => {\r
+ console.log('iceConnectionState = ' + hpc.iceConnectionState)\r
+ }\r
+ hpc.ondatachannel = (evt) => {\r
+ dataChannel = evt.channel\r
+ dataChannel.onmessage = (msg) => {\r
+ let clientMessage = JSON.parse(msg.data)\r
+ console.log(clientMessage)\r
+ hpc.setRemoteDescription(clientMessage.sdp).then(() => {\r
+ console.log('should be streaming now')\r
+ })\r
+ }\r
+ dataChannel.onopen = () => {\r
+ screenStream.getTracks().forEach( (track) => {\r
+ hpc.addTrack(track, screenStream)\r
+ })\r
+ console.log(hpc.getSenders())\r
+ /* Create offer */\r
+ hpc.createOffer().then((offer) => {\r
+ return hpc.setLocalDescription(offer)\r
+ }).then( () => {\r
+ dataChannel.send(JSON.stringify({\r
+ "cmd": "< screen dataChannel",\r
+ "sdp": hpc.localDescription,\r
+ "pubKey": hpc['hpk'].n\r
+ }))\r
+ })\r
+ }\r
+ }\r
+ hpc.onnegotiationneeded = () => {\r
+ console.log('negotiation needed')\r
+ }\r
+\r
+}\r
+\r
+function handleNewIceSubmission(msg) {\r
+ console.log('Host: Adding new ice candidate')\r
+ const hpc = clients.get(msg.pubKey)\r
+ let candidate = new RTCIceCandidate(msg.ice)\r
+ hpc.addIceCandidate(candidate)\r
+}\r
+\r
+function handleIceRequest(msg) {\r
+ console.log('Host: Handling ice candidate request')\r
+ console.log(iceCandidates)\r
+ const hpc = clients.get(msg.pubKey)\r
+ const iceCandidate = iceCandidates.pop()\r
+ if (iceCandidate !== undefined) {\r
+ wsock.send(iceCandidate)\r
+ } else {\r
+ if (hpc.iceGatheringState.localeCompare('gathering') === 0) {\r
+ wsock.send(`{"cmd" : "< ice pubKey", "clientPubKey":"${msg.pubKey}", "iceState": "g"}`)\r
+ }\r
+ else if (hpc.iceGatheringState.localeCompare('complete') === 0) {\r
+ wsock.send(`{"cmd" : "< ice pubKey", "clientPubKey":"${msg.pubKey}", "iceState": "c"}`)\r
+ }\r
+\r
+ }\r
+\r
+}\r
+if ("WebSocket" in window) {\r
+ document.addEventListener('DOMContentLoaded', (event) => {\r
+ document.body.innerHTML = '<div>Choose options for client</div> <video autoplay></video>'\r
+ navigator.mediaDevices.getUserMedia({\r
+ video : { mediaSource: "screen",\r
+ width: {max: '1920'},\r
+ height: {max: '1080'},\r
+ frameRate: {max: '10'}} })\r
+ .then(function(mediaStream) {\r
+ let video = document.querySelector('video')\r
+ screenStream = mediaStream\r
+ console.log(mediaStream)\r
+ video.srcObject = mediaStream\r
+ console.log('Grabbed media')\r
+ video.onloadedmetadata = function(e) {\r
+ console.log(e)\r
+ video.play()\r
+ }\r
+ })\r
+ .catch(function(err) {\r
+ document.body.innerHTML = 'Help me help you. Reload the page and allow screen sharing!'\r
+ console.log(err);\r
+ }); // always check for errors at the end.\r
+\r
+ wsock = new WebSocket(`${_strapp_protocol}://${window.location.hostname}:${_strapp_port}`)\r
+ wsock.onopen = () => {\r
+ console.log(`Strapped to ${_strapp_protocol}://${window.location.hostname}:${_strapp_port}`)\r
+ }\r
+\r
+ wsock.onmessage = (serverMsg) => {\r
+ /* msg is either offer or ice candidate or ice candidate request*/\r
+\r
+ /* What if data null? */\r
+ let msg = JSON.parse(serverMsg.data)\r
+\r
+ const clientID = msg.pubKey\r
+\r
+ /* TODO: redo this trash */\r
+ if (clients.has(clientID)) {\r
+ if (msg.ice) {\r
+ handleNewIceSubmission(msg)\r
+ } else if (msg.sdp) {\r
+ //handleRepeatedOffer\r
+ } else {\r
+ handleIceRequest(msg)\r
+ }\r
+ }\r
+ else {\r
+ if (msg.ice) {\r
+ console.log('Host: Client that doesnt exist is sending ice submissions')\r
+ } else if (msg.sdp) {\r
+ handleNewClientConnection(msg)\r
+ } else {\r
+ console.log('Host: Client that doesnt exist is sending ice requests')\r
+ }\r
+ }\r
+ }\r
+\r
+\r
+\r
+ })\r
+}\r
+else {\r
+ document.addEventListener('DOMContentLoaded', () => {\r
+ document.body.innerHTML = 'Websockets not supported in your browser'\r
+ })\r
+}\r