sockets now autoreconnect after disconnection

This commit is contained in:
Manuel Araoz 2014-02-13 16:24:17 -03:00
parent 6877cbb86c
commit 42c193dfd5
6 changed files with 196 additions and 182 deletions

View File

@ -10,6 +10,7 @@ module.exports.init = function(app, io_ext) {
ios.set('log level', 1); // reduce logging ios.set('log level', 1); // reduce logging
ios.sockets.on('connection', function(socket) { ios.sockets.on('connection', function(socket) {
socket.on('subscribe', function(topic) { socket.on('subscribe', function(topic) {
console.log('someone subscribed to ' + topic);
socket.join(topic); socket.join(topic);
}); });
}); });
@ -19,33 +20,35 @@ module.exports.broadcastTx = function(tx) {
if (ios) { if (ios) {
var t = {}; var t = {};
if (typeof tx === 'string') { if (typeof tx === 'string') {
t = { txid: tx }; t = {
} txid: tx
else { };
} else {
t = tx; t = tx;
// Outputs // Outputs
var valueOut = 0; var valueOut = 0;
t.vout.forEach( function(o) { t.vout.forEach(function(o) {
valueOut += o.value * util.COIN; valueOut += o.value * util.COIN;
}); });
t.valueOut = parseInt(valueOut) / util.COIN; t.valueOut = parseInt(valueOut) / util.COIN;
} }
ios.sockets.in('inv').emit('tx', t); ios.sockets. in ('inv').emit('tx', t);
} }
}; };
module.exports.broadcastBlock = function(block) { module.exports.broadcastBlock = function(block) {
if (ios) ios.sockets.in('inv').emit('block', block); if (ios) ios.sockets. in ('inv').emit('block', block);
}; };
module.exports.broadcastAddressTx = function(address, tx) { module.exports.broadcastAddressTx = function(address, tx) {
if (ios) ios.sockets.in(address).emit(address, tx); console.log('bcatx = '+address+' '+tx);
if (ios) ios.sockets. in (address).emit(address, tx);
}; };
module.exports.broadcastSyncInfo = function(historicSync) { module.exports.broadcastSyncInfo = function(historicSync) {
if (ios) { if (ios) {
ios.sockets.in('sync').emit('status', historicSync); ios.sockets. in ('sync').emit('status', historicSync);
} }
}; };

View File

@ -1,7 +1,7 @@
'use strict'; 'use strict';
angular.module('insight.address').controller('AddressController', angular.module('insight.address').controller('AddressController',
function($scope, $rootScope, $routeParams, $location, Global, Address, getSocket) { function($scope, $rootScope, $routeParams, $location, Global, Address, getSocket) {
$scope.global = Global; $scope.global = Global;
$scope.findOne = function() { $scope.findOne = function() {
@ -11,18 +11,16 @@ function($scope, $rootScope, $routeParams, $location, Global, Address, getSocket
addrStr: $routeParams.addrStr addrStr: $routeParams.addrStr
}, },
function(address) { function(address) {
$rootScope.titleDetail = address.addrStr.substring(0,7) + '...'; $rootScope.titleDetail = address.addrStr.substring(0, 7) + '...';
$rootScope.flashMessage = null; $rootScope.flashMessage = null;
$scope.address = address; $scope.address = address;
}, },
function(e) { function(e) {
if (e.status === 400) { if (e.status === 400) {
$rootScope.flashMessage = 'Invalid Address: ' + $routeParams.addrStr; $rootScope.flashMessage = 'Invalid Address: ' + $routeParams.addrStr;
} } else if (e.status === 503) {
else if (e.status === 503) {
$rootScope.flashMessage = 'Backend Error. ' + e.data; $rootScope.flashMessage = 'Backend Error. ' + e.data;
} } else {
else {
$rootScope.flashMessage = 'Address Not Found'; $rootScope.flashMessage = 'Address Not Found';
} }
$location.path('/'); $location.path('/');
@ -30,12 +28,14 @@ function($scope, $rootScope, $routeParams, $location, Global, Address, getSocket
}; };
var socket = getSocket($scope); var socket = getSocket($scope);
socket.on('connect', function() {
socket.emit('subscribe', $routeParams.addrStr); socket.emit('subscribe', $routeParams.addrStr);
socket.on($routeParams.addrStr, function(tx) { socket.on($routeParams.addrStr, function(tx) {
console.log('AddressTx event received ' + tx); console.log('AddressTx event received ' + tx);
$rootScope.$broadcast('tx', tx); $rootScope.$broadcast('tx', tx);
}); });
});
$scope.params = $routeParams; $scope.params = $routeParams;
}); });

View File

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

View File

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

View File

@ -1,7 +1,7 @@
'use strict'; 'use strict';
angular.module('insight.status').controller('StatusController', angular.module('insight.status').controller('StatusController',
function($scope, $routeParams, $location, Global, Status, Sync, getSocket) { function($scope, $routeParams, $location, Global, Status, Sync, getSocket) {
$scope.global = Global; $scope.global = Global;
$scope.getStatus = function(q) { $scope.getStatus = function(q) {
@ -28,14 +28,17 @@ function($scope, $routeParams, $location, Global, Status, Sync, getSocket) {
}, },
function(e) { function(e) {
var err = 'Could not get sync information' + e.toString(); var err = 'Could not get sync information' + e.toString();
$scope.sync = { error: err }; $scope.sync = {
error: err
};
}); });
}; };
var socket = getSocket($scope); var socket = getSocket($scope);
socket.on('connect', function() {
socket.emit('subscribe', 'sync'); socket.emit('subscribe', 'sync');
socket.on('status', function(sync) { socket.on('status', function(sync) {
_onSyncUpdate(sync); _onSyncUpdate(sync);
}); });
}); });
});

View File

@ -6,9 +6,13 @@ var ScopedSocket = function(socket, $rootScope) {
this.listeners = []; this.listeners = [];
}; };
ScopedSocket.prototype.removeAllListeners = function() { ScopedSocket.prototype.removeAllListeners = function(opts) {
if (!opts) opts = {};
for (var i = 0; i < this.listeners.length; i++) { for (var i = 0; i < this.listeners.length; i++) {
var details = this.listeners[i]; var details = this.listeners[i];
if (opts.skipConnect && details.event === 'connect') {
continue;
}
this.socket.removeListener(details.event, details.fn); this.socket.removeListener(details.event, details.fn);
} }
this.listeners = []; this.listeners = [];
@ -47,18 +51,21 @@ ScopedSocket.prototype.emit = function(event, data, callback) {
}; };
angular.module('insight.socket').factory('getSocket', angular.module('insight.socket').factory('getSocket',
function($rootScope) { function($rootScope) {
var socket = io.connect(null, { var socket = io.connect(null, {
'reconnect': true, 'reconnect': true,
'reconnection delay': 500, 'reconnection delay': 500,
}); });
return function(scope) { return function(scope) {
var scopedSocket = new ScopedSocket(socket, $rootScope); var scopedSocket = new ScopedSocket(socket, $rootScope);
scope.$on('$routeChangeStart', function() {
});
scope.$on('$destroy', function() { scope.$on('$destroy', function() {
scopedSocket.removeAllListeners(); scopedSocket.removeAllListeners();
}); });
socket.on('connect', function() {
scopedSocket.removeAllListeners({
skipConnect: true
});
});
return scopedSocket; return scopedSocket;
}; };
}); });