Merge pull request #335 from Bitcoin-com/wallet/task/586
Bug - 586 - Reconnect WebSocket when connection is failing or lost.
This commit is contained in:
commit
e7eac1ee16
1 changed files with 60 additions and 39 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({
|
||||||
|
|
@ -24,6 +24,50 @@ angular.module('copayApp.controllers').controller('tabReceiveController', functi
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
function connectSocket() {
|
||||||
|
// Close existing socket if not connected with current address
|
||||||
|
if (currentAddressSocket) {
|
||||||
|
currentAddressSocket.close([CLOSE_NORMAL]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if ($scope.wallet.coin === 'bch') {
|
||||||
|
// listen to bch address
|
||||||
|
currentAddressSocket = new WebSocket('wss://ws.blockchain.info/bch/inv');
|
||||||
|
paymentSubscriptionObj.addr = $scope.addrBchLegacy;
|
||||||
|
} else {
|
||||||
|
// listen to btc address
|
||||||
|
currentAddressSocket = new WebSocket('wss://ws.blockchain.info/inv');
|
||||||
|
paymentSubscriptionObj.addr = $scope.addr;
|
||||||
|
}
|
||||||
|
|
||||||
|
// create subscription to address
|
||||||
|
var msg = JSON.stringify(paymentSubscriptionObj);
|
||||||
|
currentAddressSocket.onopen = function (event) {
|
||||||
|
currentAddressSocket.send(msg);
|
||||||
|
};
|
||||||
|
|
||||||
|
// listen for response
|
||||||
|
currentAddressSocket.onmessage = function (event) {
|
||||||
|
//console.log("message received:" + event.data);
|
||||||
|
receivedPayment(event.data);
|
||||||
|
};
|
||||||
|
|
||||||
|
currentAddressSocket.onclose = function(event) {
|
||||||
|
if (event.code !== CLOSE_NORMAL) {
|
||||||
|
$log.debug('Socket was closed abnormally. Reconnect will be attempted in 1 second.');
|
||||||
|
$timeout(function() {
|
||||||
|
connectSocket();
|
||||||
|
}, 1000);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
currentAddressSocket.onerror = function(err) {
|
||||||
|
console.error('Socket encountered error: ', err, 'Closing socket');
|
||||||
|
currentAddressSocket.close();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
$scope.setAddress = function(newAddr, copyAddress) {
|
$scope.setAddress = function(newAddr, copyAddress) {
|
||||||
$scope.addr = null;
|
$scope.addr = null;
|
||||||
if (!$scope.wallet || $scope.generatingAddress || !$scope.wallet.isComplete()) return;
|
if (!$scope.wallet || $scope.generatingAddress || !$scope.wallet.isComplete()) return;
|
||||||
|
|
@ -36,49 +80,24 @@ angular.module('copayApp.controllers').controller('tabReceiveController', functi
|
||||||
popupService.showAlert(err);
|
popupService.showAlert(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
//close existing socket
|
if ($scope.wallet.coin === 'bch') {
|
||||||
if (currentAddressSocket.close === 'function') {
|
bchAddresses = bitcoinCashJsService.translateAddresses(addr);
|
||||||
currentAddressSocket.close();
|
$scope.addr = bchAddresses[$scope.bchAddressType.type];
|
||||||
}
|
$scope.addrBchLegacy = bchAddresses['legacy'];
|
||||||
|
|
||||||
if ($scope.wallet.coin == 'bch') {
|
|
||||||
bchAddresses = bitcoinCashJsService.translateAddresses(addr);
|
|
||||||
$scope.addr = bchAddresses[$scope.bchAddressType.type];
|
|
||||||
$scope.addrBchLegacy = bchAddresses['legacy'];
|
|
||||||
|
|
||||||
// listen to bch address
|
|
||||||
currentAddressSocket = new WebSocket("wss://ws.blockchain.info/bch/inv");
|
|
||||||
paymentSubscriptionObj.addr = bchAddresses['legacy'];
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
$scope.addr = addr;
|
$scope.addr = addr;
|
||||||
|
|
||||||
// listen to btc address
|
|
||||||
currentAddressSocket = new WebSocket("wss://ws.blockchain.info/inv");
|
|
||||||
paymentSubscriptionObj.addr = $scope.addr
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
connectSocket();
|
||||||
|
|
||||||
if (copyAddress === true) {
|
if (copyAddress === true) {
|
||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// create subscription
|
|
||||||
var msg = JSON.stringify(paymentSubscriptionObj);
|
|
||||||
currentAddressSocket.onopen = function (event) {
|
|
||||||
//console.log("message sent: " + msg);
|
|
||||||
currentAddressSocket.send(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
// listen for response
|
|
||||||
currentAddressSocket.onmessage = function (event) {
|
|
||||||
//console.log("message received:" + event.data);
|
|
||||||
receivedPayment(event.data);
|
|
||||||
}
|
|
||||||
|
|
||||||
$timeout(function() {
|
$timeout(function() {
|
||||||
$scope.$apply();
|
$scope.$apply();
|
||||||
|
|
@ -164,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;
|
||||||
});
|
});
|
||||||
|
|
@ -233,6 +251,10 @@ angular.module('copayApp.controllers').controller('tabReceiveController', functi
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
function onBeforeLeave() {
|
||||||
|
currentAddressSocket.close([CLOSE_NORMAL]);
|
||||||
|
}
|
||||||
|
|
||||||
$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;
|
||||||
|
|
@ -258,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