Merge pull request #300 from maraoz/feature/socket-reconnect

Feature/socket reconnect
This commit is contained in:
Mario Colque 2014-02-13 17:37:35 -02:00
commit ac33b5c659
6 changed files with 195 additions and 182 deletions

View File

@ -19,9 +19,10 @@ module.exports.broadcastTx = function(tx) {
if (ios) {
var t = {};
if (typeof tx === 'string') {
t = { txid: tx };
}
else {
t = {
txid: tx
};
} else {
t = tx;
// Outputs
var valueOut = 0;
@ -40,6 +41,7 @@ module.exports.broadcastBlock = function(block) {
};
module.exports.broadcastAddressTx = function(address, tx) {
console.log('bcatx = '+address+' '+tx);
if (ios) ios.sockets. in (address).emit(address, tx);
};

View File

@ -18,11 +18,9 @@ function($scope, $rootScope, $routeParams, $location, Global, Address, getSocket
function(e) {
if (e.status === 400) {
$rootScope.flashMessage = 'Invalid Address: ' + $routeParams.addrStr;
}
else if (e.status === 503) {
} else if (e.status === 503) {
$rootScope.flashMessage = 'Backend Error. ' + e.data;
}
else {
} else {
$rootScope.flashMessage = 'Address Not Found';
}
$location.path('/');
@ -30,11 +28,13 @@ function($scope, $rootScope, $routeParams, $location, Global, Address, getSocket
};
var socket = getSocket($scope);
socket.on('connect', function() {
socket.emit('subscribe', $routeParams.addrStr);
socket.on($routeParams.addrStr, function(tx) {
console.log('AddressTx event received ' + tx);
$rootScope.$broadcast('tx', tx);
});
});
$scope.params = $routeParams;

View File

@ -10,19 +10,13 @@ angular.module('insight.system').controller('HeaderController',
symbol: 'BTC'
};
$scope.menu = [
{
$scope.menu = [{
'title': 'Blocks',
'link': 'blocks'
},
{
}, {
'title': 'Status',
'link': 'status'
}
];
var socket = getSocket($scope);
socket.emit('subscribe', 'inv');
}];
var _getBlock = function(hash) {
Block.get({
@ -32,11 +26,15 @@ angular.module('insight.system').controller('HeaderController',
});
};
var socket = getSocket($scope);
socket.on('connect', function() {
socket.emit('subscribe', 'inv');
socket.on('block', function(block) {
var blockHash = block.toString();
console.log('Updated Blocks Height!');
_getBlock(blockHash);
});
});
$rootScope.isCollapsed = true;
});

View File

@ -17,6 +17,7 @@ angular.module('insight.system').controller('IndexController',
};
var socket = getSocket($scope);
socket.on('connect', function() {
socket.emit('subscribe', 'inv');
socket.on('tx', function(tx) {
@ -30,6 +31,8 @@ angular.module('insight.system').controller('IndexController',
_getBlocks();
});
});
$scope.humanSince = function(time) {
var m = moment.unix(time);
return m.max().fromNow();

View File

@ -28,14 +28,17 @@ function($scope, $routeParams, $location, Global, Status, Sync, getSocket) {
},
function(e) {
var err = 'Could not get sync information' + e.toString();
$scope.sync = { error: err };
$scope.sync = {
error: err
};
});
};
var socket = getSocket($scope);
socket.on('connect', function() {
socket.emit('subscribe', 'sync');
socket.on('status', function(sync) {
_onSyncUpdate(sync);
});
});
});

View File

@ -6,9 +6,13 @@ var ScopedSocket = function(socket, $rootScope) {
this.listeners = [];
};
ScopedSocket.prototype.removeAllListeners = function() {
ScopedSocket.prototype.removeAllListeners = function(opts) {
if (!opts) opts = {};
for (var i = 0; i < this.listeners.length; i++) {
var details = this.listeners[i];
if (opts.skipConnect && details.event === 'connect') {
continue;
}
this.socket.removeListener(details.event, details.fn);
}
this.listeners = [];
@ -54,11 +58,14 @@ function($rootScope) {
});
return function(scope) {
var scopedSocket = new ScopedSocket(socket, $rootScope);
scope.$on('$routeChangeStart', function() {
});
scope.$on('$destroy', function() {
scopedSocket.removeAllListeners();
});
socket.on('connect', function() {
scopedSocket.removeAllListeners({
skipConnect: true
});
});
return scopedSocket;
};
});