Merge pull request #392 from yemel/fix/handle-sudden-disconnection

Fix/handle sudden disconnection
This commit is contained in:
Gustavo Maximiliano Cortez 2014-05-16 09:42:51 -03:00
commit 66c1588fef
4 changed files with 33 additions and 8 deletions

View File

@ -162,10 +162,14 @@
<!-- Templates --> <!-- Templates -->
<script type="text/ng-template" id="signin.html"> <script type="text/ng-template" id="signin.html">
<div class="signin" ng-controller="SigninController"> <div class="signin" ng-controller="SigninController">
<div data-alert class="alert-box info radius" ng-show="loading"> <div data-alert class="alert-box info radius" ng-show="loading && !failure">
<i class="size-21 fi-bitcoin-circle icon-rotate spinner"></i> <i class="size-21 fi-bitcoin-circle icon-rotate spinner"></i>
Authenticating and Looking for peers... Authenticating and Looking for peers...
</div> </div>
<div class="alert-box error radius" ng-show="failure">
Oops, we had an error! Looks like you are already connected to this wallet,
please close all other Copay Wallets and <a ng-click='failure = false; loading = false'>Try again</a>.
</div>
<div ng-show="!loading"> <div ng-show="!loading">
<div class="row"> <div class="row">
<div class="large-6 medium-6 columns"> <div class="large-6 medium-6 columns">

View File

@ -44,11 +44,7 @@ angular.module('copay.header').controller('HeaderController',
}; };
$scope.signout = function() { $scope.signout = function() {
var w = $rootScope.wallet; logout();
if (w) {
w.disconnect();
controllerUtils.logout();
}
$scope.clearFlashMessage(); $scope.clearFlashMessage();
}; };
@ -64,4 +60,19 @@ angular.module('copay.header').controller('HeaderController',
}; };
$rootScope.isCollapsed = true; $rootScope.isCollapsed = true;
function logout() {
var w = $rootScope.wallet;
if (w) {
w.disconnect();
controllerUtils.logout();
}
}
// Ensures a graceful disconnect
window.onbeforeunload = logout;
$scope.$on('$destroy', function() {
window.onbeforeunload = undefined;
});
}); });

View File

@ -7,7 +7,7 @@ angular.module('copay.signin').controller('SigninController',
return v1 > v2 ? 1 : ( v1 < v2 ) ? -1 : 0; return v1 > v2 ? 1 : ( v1 < v2 ) ? -1 : 0;
}; };
$scope.loading = false; $scope.loading = $scope.failure = false;
$scope.wallets = walletFactory.getWallets().sort(cmp); $scope.wallets = walletFactory.getWallets().sort(cmp);
$scope.selectedWalletId = $scope.wallets.length ? $scope.wallets[0].id : null; $scope.selectedWalletId = $scope.wallets.length ? $scope.wallets[0].id : null;
$scope.openPassword = ''; $scope.openPassword = '';
@ -34,12 +34,13 @@ angular.module('copay.signin').controller('SigninController',
Passphrase.getBase64Async(password, function(passphrase){ Passphrase.getBase64Async(password, function(passphrase){
var w = walletFactory.open($scope.selectedWalletId, { passphrase: passphrase}); var w = walletFactory.open($scope.selectedWalletId, { passphrase: passphrase});
if (!w) { if (!w) {
$scope.loading = false; $scope.loading = $scope.failure = false;
$rootScope.flashMessage = { message: 'Bad password or connection error', type: 'error'}; $rootScope.flashMessage = { message: 'Bad password or connection error', type: 'error'};
$rootScope.$digest(); $rootScope.$digest();
return; return;
} }
controllerUtils.startNetwork(w); controllerUtils.startNetwork(w);
listenErrors(w);
}); });
}; };
@ -66,8 +67,16 @@ angular.module('copay.signin').controller('SigninController',
controllerUtils.onErrorDigest(); controllerUtils.onErrorDigest();
} else { } else {
controllerUtils.startNetwork(w); controllerUtils.startNetwork(w);
listenErrors(w);
} }
}); });
}); });
}; };
function listenErrors(wallet) {
wallet.network.on('error', function(err) {
$scope.failure = true;
});
}
}); });

View File

@ -262,6 +262,7 @@ Network.prototype._setupPeerHandlers = function(openCallback) {
p.on('error', function(err) { p.on('error', function(err) {
if (!err.message.match(/Could\snot\sconnect\sto peer/)) { if (!err.message.match(/Could\snot\sconnect\sto peer/)) {
console.log('### PEER ERROR:', err); console.log('### PEER ERROR:', err);
self.emit('error', err);
} }
self._checkAnyPeer(); self._checkAnyPeer();
}); });