Skip to content

Commit

Permalink
double refresh fix
Browse files Browse the repository at this point in the history
  • Loading branch information
abhishekgahlot committed Jul 23, 2017
1 parent 47821d8 commit 241f300
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 14 deletions.
17 changes: 8 additions & 9 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
} );

/**
Expand Down Expand Up @@ -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();
} );
} );
Expand Down
16 changes: 12 additions & 4 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion static/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 241f300

Please sign in to comment.