Reconnecting socket based on CloseEvent.code.
This commit is contained in:
parent
6692812d55
commit
a3614566e3
1 changed files with 22 additions and 32 deletions
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
angular.module('copayApp.controllers').controller('tabReceiveController', function($rootScope, $scope, $timeout, $log, $ionicModal, $state, $ionicHistory, $ionicPopover, storageService, platformInfo, walletService, profileService, configService, lodash, gettextCatalog, popupService, bwcError, bitcoinCashJsService, $ionicNavBarDelegate, sendFlowService, txFormatService, soundService, clipboardService) {
|
angular.module('copayApp.controllers').controller('tabReceiveController', function($rootScope, $scope, $timeout, $log, $ionicModal, $state, $ionicHistory, $ionicPopover, storageService, platformInfo, walletService, profileService, configService, lodash, gettextCatalog, popupService, bwcError, bitcoinCashJsService, $ionicNavBarDelegate, sendFlowService, txFormatService, soundService, clipboardService) {
|
||||||
|
|
||||||
|
var CLOSE_NORMAL = 1000;
|
||||||
var listeners = [];
|
var listeners = [];
|
||||||
$scope.bchAddressType = { type: 'cashaddr' };
|
$scope.bchAddressType = { type: 'cashaddr' };
|
||||||
var bchAddresses = {};
|
var bchAddresses = {};
|
||||||
|
|
@ -10,12 +11,11 @@ angular.module('copayApp.controllers').controller('tabReceiveController', functi
|
||||||
$scope.isCordova = platformInfo.isCordova;
|
$scope.isCordova = platformInfo.isCordova;
|
||||||
$scope.isNW = platformInfo.isNW;
|
$scope.isNW = platformInfo.isNW;
|
||||||
|
|
||||||
var currentAddressSocket = {};
|
var currentAddressSocket = null;
|
||||||
var paymentSubscriptionObj = { op:"addr_sub" }
|
var paymentSubscriptionObj = { op:'addr_sub' };
|
||||||
|
|
||||||
var config;
|
|
||||||
|
|
||||||
$scope.displayBalanceAsFiat = true;
|
$scope.displayBalanceAsFiat = true;
|
||||||
|
$scope.$on('$ionicView.beforeLeave', onBeforeLeave);
|
||||||
|
|
||||||
$scope.requestSpecificAmount = function() {
|
$scope.requestSpecificAmount = function() {
|
||||||
sendFlowService.start({
|
sendFlowService.start({
|
||||||
|
|
@ -26,18 +26,18 @@ angular.module('copayApp.controllers').controller('tabReceiveController', functi
|
||||||
|
|
||||||
|
|
||||||
function connectSocket() {
|
function connectSocket() {
|
||||||
//close existing socket if not connected with current address
|
// Close existing socket if not connected with current address
|
||||||
if (currentAddressSocket && typeof currentAddressSocket.close === 'function') {
|
if (currentAddressSocket) {
|
||||||
currentAddressSocket.onclose = function(e) {}; // Overwrite onclose-function to prevent reconnecting old address socket.
|
currentAddressSocket.close([CLOSE_NORMAL]);
|
||||||
currentAddressSocket.close();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($scope.wallet.coin === 'bch') {
|
if ($scope.wallet.coin === 'bch') {
|
||||||
// listen to bch address
|
// listen to bch address
|
||||||
currentAddressSocket = new WebSocket("wss://ws.blockchain.info/bch/inv");
|
currentAddressSocket = new WebSocket('wss://ws.blockchain.info/bch/inv');
|
||||||
paymentSubscriptionObj.addr = $scope.addrBchLegacy;
|
paymentSubscriptionObj.addr = $scope.addrBchLegacy;
|
||||||
} else {
|
} else {
|
||||||
// listen to btc address
|
// listen to btc address
|
||||||
currentAddressSocket = new WebSocket("wss://ws.blockchain.info/inv");
|
currentAddressSocket = new WebSocket('wss://ws.blockchain.info/inv');
|
||||||
paymentSubscriptionObj.addr = $scope.addr;
|
paymentSubscriptionObj.addr = $scope.addr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -53,21 +53,19 @@ angular.module('copayApp.controllers').controller('tabReceiveController', functi
|
||||||
receivedPayment(event.data);
|
receivedPayment(event.data);
|
||||||
};
|
};
|
||||||
|
|
||||||
currentAddressSocket.onclose = function(e) {
|
currentAddressSocket.onclose = function(event) {
|
||||||
console.log('Socket is closed. Reconnect will be attempted in 1 second.');
|
if (event.code !== CLOSE_NORMAL) {
|
||||||
$timeout(function() {
|
$log.debug('Socket was closed abnormally. Reconnect will be attempted in 1 second.');
|
||||||
connectSocket();
|
$timeout(function() {
|
||||||
}, 1000);
|
connectSocket();
|
||||||
|
}, 1000);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
currentAddressSocket.onerror = function(err) {
|
currentAddressSocket.onerror = function(err) {
|
||||||
console.error('Socket encountered error: ', err, 'Closing socket');
|
console.error('Socket encountered error: ', err, 'Closing socket');
|
||||||
currentAddressSocket.close();
|
currentAddressSocket.close();
|
||||||
};
|
};
|
||||||
|
|
||||||
$timeout(function() {
|
|
||||||
$scope.$apply();
|
|
||||||
}, 10);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$scope.setAddress = function(newAddr, copyAddress) {
|
$scope.setAddress = function(newAddr, copyAddress) {
|
||||||
|
|
@ -96,7 +94,7 @@ angular.module('copayApp.controllers').controller('tabReceiveController', functi
|
||||||
try {
|
try {
|
||||||
clipboardService.copyToClipboard($scope.wallet.coin == 'bch' && $scope.bchAddressType.type == 'cashaddr' ? 'bitcoincash:' + $scope.addr : $scope.addr);
|
clipboardService.copyToClipboard($scope.wallet.coin == 'bch' && $scope.bchAddressType.type == 'cashaddr' ? 'bitcoincash:' + $scope.addr : $scope.addr);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
$log.debug("Error copying to clipboard:");
|
$log.debug('Error copying to clipboard:');
|
||||||
$log.debug(error);
|
$log.debug(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -185,7 +183,6 @@ angular.module('copayApp.controllers').controller('tabReceiveController', functi
|
||||||
// Notify new tx
|
// Notify new tx
|
||||||
$scope.$emit('bwsEvent', $scope.wallet.id);
|
$scope.$emit('bwsEvent', $scope.wallet.id);
|
||||||
|
|
||||||
|
|
||||||
$scope.$apply(function () {
|
$scope.$apply(function () {
|
||||||
$scope.showingPaymentReceived = true;
|
$scope.showingPaymentReceived = true;
|
||||||
});
|
});
|
||||||
|
|
@ -254,16 +251,10 @@ angular.module('copayApp.controllers').controller('tabReceiveController', functi
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
$scope.$on("$ionicView.beforeLeave", function() {
|
function onBeforeLeave() {
|
||||||
// Close the old connection!
|
currentAddressSocket.close([CLOSE_NORMAL]);
|
||||||
if (currentAddressSocket && typeof currentAddressSocket.close === 'function') {
|
}
|
||||||
console.log("Close open websocket address connection.");
|
|
||||||
currentAddressSocket.onclose = function(e) {}; // Overwrite onclose-function to prevent reconnecting old address socket.
|
|
||||||
currentAddressSocket.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
});
|
|
||||||
|
|
||||||
$scope.$on("$ionicView.beforeEnter", function(event, data) {
|
$scope.$on("$ionicView.beforeEnter", function(event, data) {
|
||||||
$scope.wallets = profileService.getWallets();
|
$scope.wallets = profileService.getWallets();
|
||||||
$scope.singleWallet = $scope.wallets.length == 1;
|
$scope.singleWallet = $scope.wallets.length == 1;
|
||||||
|
|
@ -289,7 +280,6 @@ angular.module('copayApp.controllers').controller('tabReceiveController', functi
|
||||||
|
|
||||||
configService.whenAvailable(function(_config) {
|
configService.whenAvailable(function(_config) {
|
||||||
$scope.displayBalanceAsFiat = _config.wallet.settings.priceDisplay === 'fiat';
|
$scope.displayBalanceAsFiat = _config.wallet.settings.priceDisplay === 'fiat';
|
||||||
config = _config;
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue