From 241f3000c028831192978667e245f259b8c7f590 Mon Sep 17 00:00:00 2001 From: Abhishek Gahlot Date: Thu, 20 Jul 2017 18:05:29 +0530 Subject: [PATCH] double refresh fix --- app.js | 17 ++++++++--------- server.js | 16 ++++++++++++---- static/index.html | 2 +- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/app.js b/app.js index c671204..3271da9 100644 --- a/app.js +++ b/app.js @@ -125,10 +125,10 @@ class TransportLayer { decrypt( encrypted ) { const iv = crypto.forge.util.createBuffer(); const data = crypto.forge.util.createBuffer(); - iv.putBytes( crypto.forge.util.hexToBytes( encrypted.iv )); - data.putBytes( crypto.forge.util.hexToBytes( encrypted.encrypted )); + iv.putBytes( crypto.forge.util.hexToBytes( encrypted.iv ) ); + data.putBytes( crypto.forge.util.hexToBytes( encrypted.encrypted ) ); - const decipher = crypto.forge.cipher.createDecipher('AES-CBC', this.key ); + const decipher = crypto.forge.cipher.createDecipher( 'AES-CBC', this.key ); decipher.start( { iv } ); decipher.update( data ); decipher.finish(); @@ -144,10 +144,11 @@ class GRTC extends EventEmitter { /** * @param {string} grtcID global id representing document. * @param {string} url url of signal routes. + * @param {string} peerName name of user/ wordpress username. * @param {bool} useTransport default false for encrypted session. * uuid is uniquely generated id for collaboration to happen */ - constructor( grtcID, url, useTransport ) { + constructor( grtcID, url, peerName, useTransport ) { super(); const self = this; self.peer = null; @@ -156,7 +157,7 @@ class GRTC extends EventEmitter { self.url = url; self.grtcID = grtcID; self.peerID = GRTC.uuid(); - self.peerName = window.localStorage.getItem( 'username' ); + self.peerName = peerName; self.otherPeers = new Set(); self.listenSignalTimer = 0; self.listenSignalCount = 0; @@ -333,9 +334,7 @@ class GRTC extends EventEmitter { clearInterval( self.listenSignalTimer ); delete self._events.initiator; delete self._events.peerFound; - self.signalInstance.clearSignal().then( () => { - self.init(); - } ); + self.init(); } ); /** @@ -442,7 +441,7 @@ class GRTC extends EventEmitter { */ self.peerHandler().then( () => { self.signalInstance = new Signal( self.url, self.grtcID, self.peerID, self.peerName, self.peerSignal ); - self.signalInstance.updateSignal().then( ( data ) => { + self.signalInstance.updateSignal().then( () => { self.listenSignal(); } ); } ); diff --git a/server.js b/server.js index 2922d4a..9152c9c 100644 --- a/server.js +++ b/server.js @@ -58,16 +58,24 @@ app.post('/set', (req, res) => { res.send(data); } else if (store.length && type === 'initial') { let exists = false; + let data = {}; + store.forEach((peer) => { - if (peer.peerID == peerID) { - res.send(peer); + if (peer.peerName === peerName) { + peer.peerID = peerID; exists = true; + peer.signal = false; + data = peer; } }); - if (exists) { return; } + if (exists) { + kv.put(key, store); + res.send(data); + return; + } - let data = { + data = { peerID, type, peerName, diff --git a/static/index.html b/static/index.html index 22b3865..1f94d84 100644 --- a/static/index.html +++ b/static/index.html @@ -28,7 +28,7 @@ if (grtcID.length == 0) { var grtcID = GRTC.uuid(); window.history.pushState('', '', '?collaborate=' + grtcID); - var grtc = new GRTC(grtcID, window.location.origin); + var grtc = new GRTC(grtcID, 'http://localhost' + '/wp-json/collaborate'); } else { var grtc = new GRTC(grtcID, window.location.origin); }