document.title = "Strapp.io Host"
-const conf = {"iceServers": [{ "url": "stun:stun.1.google.com:19302" }] }
+
+
+const conf = {"iceServers": [{ "urls": "stun:stun.1.google.com:19302" }] }
const clients = new Map([])
-const hpk = getPublicKey()
+const iceCandidates = []
+let dataChannel
+
/* TODO: duplicate in both client.js and host.jhs */
function getPublicKey() {
})
}
-
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(() => {
- hpk.then(() => {
+ getPublicKey().then((hpk) => {
hpc.onicecandidate = (event) => {
if (event.candidate) {
- console.log('Host: Sending ice candidate to client')
- wsock.send(JSON.stringify({
- cmd: '< ice pubKey',
+ console.log('Host: Allocating ice candidate for client')
+ iceCandidates.push(JSON.stringify({
+ cmd: "< ice pubKey",
ice: event.candidate,
- pubKey: hpk /* TODO: do we need to send this? */
+ hostPubKey: hpk.n, /* TODO: do we need to send this? */
+ clientPubKey: offer.pubKey,
+ iceState: "a"
}))
}
else {
- console.log('Host: Finished setting up ICE candidates')
+ console.log('Host: Finished sending ICE candidates')
+ console.log(hpc)
}
}
- console.log('Host: Sending answer to Host')
+ console.log('Host: Sending answer to Client')
wsock.send(JSON.stringify({
cmd: '< sdp pubKey',
sdp: hpc.localDescription,
- pubKey: hpk
+ hostPubKey: hpk.n,
+ clientPubKey: offer.pubKey
}))
- clients.set(offer.pubKey, {
- hostsdp: hpc.localDescription,
- clientsdp: hpc.remoteDescription
- })
-
+ 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 handleNewIceCandidate(msg) {
+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.pubKey)
+ const iceCandidate = iceCandidates.pop()
+ if (iceCandidate !== undefined) {
+ wsock.send(iceCandidate)
+ } else {
+ if (hpc.iceGatheringState.localeCompare('gathering') === 0) {
+ wsock.send(`{"cmd" : "< ice pubKey", "clientPubKey":"${msg.pubKey}", "iceState": "g"}`)
+ }
+ else if (hpc.iceGatheringState.localeCompare('complete') === 0) {
+ wsock.send(`{"cmd" : "< ice pubKey", "clientPubKey":"${msg.pubKey}", "iceState": "c"}`)
+ }
+
+ }
+
+}
if ("WebSocket" in window) {
document.addEventListener('DOMContentLoaded', (event) => {
wsock = new WebSocket(`${_strapp_protocol}://${window.location.hostname}:${_strapp_port}`)
}
wsock.onmessage = (serverMsg) => {
- /* msg is either offer or ice candidate */
- console.log(serverMsg.data)
+ /* 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
- const msgType = msg.hasOwnProperty('sdp') ? 'o' : 'i'
+
+ /* TODO: redo this trash */
if (clients.has(clientID)) {
- switch(msgType) {
- case 'o':
- console.log('client exist && sending an offer == error')
- break
- case 'i':
- handleNewIceCandidate(msg)
- break
+ if (msg.ice) {
+ handleNewIceSubmission(msg)
+ } else if (msg.sdp) {
+ //handleRepeatedOffer
+ } else {
+ handleIceRequest(msg)
}
}
else {
- switch(msgType) {
- case 'o':
+ if (msg.ice) {
+ console.log('Host: Client that doesnt exist is sending ice submissions')
+ } else if (msg.sdp) {
handleNewClientConnection(msg)
- break
- case 'i':
- console.log('client !exist && ice candidate === error')
- break
+ } else {
+ console.log('Host: Client that doesnt exist is sending ice requests')
}
}
}
+ document.body.innerHTML = '<div>Choose options for client</div> <video autoplay></video>'
+
+ navigator.mediaDevices.getUserMedia({ video : { mediaSource: "screen", // whole screen sharing
+ // mediaSource: "window", // choose a window to share
+ // mediaSource: "application", // choose a window to share
+ width: {max: '1920'},
+ height: {max: '1080'},
+ frameRate: {max: '10'}} })
+ .then(function(mediaStream) {
+ let video = document.querySelector('video')
+ video.srcObject = mediaStream
+ video.onloadedmetadata = function(e) {
+ video.play()
+ }
+ })
+ .catch(function(err) { console.log(err); }); // always check for errors at the end.
})
}
else {