+ 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}`)