Merge pull request #589 from yemel/feature/peerjs-reconnect

Add retry connection to peerjs server on login and peerjs server error handling
This commit is contained in:
Matias Alejo Garcia 2014-06-06 17:42:22 -03:00
commit 3f279b9708
4 changed files with 38 additions and 5 deletions

View File

@ -25,6 +25,10 @@ angular.module('copayApp.controllers').controller('ImportController',
var message = "Looks like you are already connected to this wallet, please logout from it and try importing it again.";
$rootScope.$flashMessage = { message: message, type: 'error'};
});
$rootScope.wallet.on('serverError', function() {
$rootScope.$flashMessage = { message: 'The PeerJS server is not responding, please try again', type: 'error'};
controllerUtils.onErrorDigest();
});
});
};

View File

@ -61,7 +61,7 @@ angular.module('copayApp.controllers').controller('SigninController',
$rootScope.$flashMessage = { message: 'Bad secret secret string', type: 'error'};
else
$rootScope.$flashMessage = { message: 'Unknown error', type: 'error'};
controllerUtils.onErrorDigest();
controllerUtils.onErrorDigest();
} else {
controllerUtils.startNetwork(w);
installStartupHandlers(w);
@ -77,6 +77,10 @@ angular.module('copayApp.controllers').controller('SigninController',
wallet.on('ready', function() {
$scope.loading = false;
});
wallet.on('serverError', function() {
$rootScope.$flashMessage = { message: 'The PeerJS server is not responding, please try again', type: 'error'};
controllerUtils.onErrorDigest($scope);
});
}
});

View File

@ -242,6 +242,9 @@ Wallet.prototype.netStart = function() {
net.on('close', function() {
self.emit('close');
});
net.on('serverError', function() {
self.emit('serverError');
});
var myId = self.getMyCopayerId();
var startOpts = {

View File

@ -52,9 +52,11 @@ Network.prototype.cleanUp = function() {
if (this.peer) {
this.peer.disconnect();
this.peer.destroy();
this.peer.removeAllListeners();
this.peer = null;
}
this.closing = 0;
this.tries = 0;
};
Network.parent=EventEmitter;
@ -309,11 +311,31 @@ Network.prototype.start = function(opts, openCallback) {
if (!this.copayerId)
this.setCopayerId(opts.copayerId);
this.peer = new Peer(this.peerId, this.opts);
this.started = true;
this._setupPeerHandlers(openCallback);
};
var self = this;
var setupPeer = function () {
if (self.connectedPeers.length > 0) return; // Already connected!
if (self.peer) {
self.peer.destroy();
self.peer.removeAllListeners();
}
if (self.tries < 2) {
self.tries++;
self.peer = new Peer(self.peerId, self.opts);
self.started = true;
self._setupPeerHandlers(openCallback);
setTimeout(setupPeer, 3000); // Schedule retry
return;
}
self.emit('serverError');
self.cleanUp();
}
this.tries = 0;
setupPeer();
};
Network.prototype.getOnlinePeerIDs = function() {
return this.connectedPeers;