socket.io and angular proper integration

This commit is contained in:
Manuel Araoz 2014-01-20 13:31:37 -03:00
parent b7d3666249
commit cf5aea1165
6 changed files with 76 additions and 42 deletions

View File

@ -9,6 +9,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('subscribe to '+topic);
socket.join(topic); socket.join(topic);
}); });
}); });

View File

@ -7,8 +7,8 @@ angular.module('insight.address').controller('AddressController',
'$location', '$location',
'Global', 'Global',
'Address', 'Address',
'socket', 'get_socket',
function ($scope, $rootScope, $routeParams, $location, Global, Address, socket) { function ($scope, $rootScope, $routeParams, $location, Global, Address, get_socket) {
$scope.global = Global; $scope.global = Global;
$scope.findOne = function() { $scope.findOne = function() {
@ -21,9 +21,8 @@ angular.module('insight.address').controller('AddressController',
$location.path('/'); $location.path('/');
}); });
}; };
socket.on('connect', function() { var socket = get_socket($scope);
socket.emit('subscribe', $routeParams.addrStr); socket.emit('subscribe', $routeParams.addrStr);
});
$scope.params = $routeParams; $scope.params = $routeParams;
}]); }]);

View File

@ -1,13 +1,12 @@
'use strict'; 'use strict';
angular.module('insight.system').controller('FooterController', ['$scope', 'Global', 'socket', 'Status', function ($scope, Global, socket, Status) { angular.module('insight.system').controller('FooterController',
['$scope',
'Global',
'Status',
function ($scope, Global, Status) {
$scope.global = Global; $scope.global = Global;
socket.on('block', function(block) {
console.log('[footer.js:14]',block); //TODO
console.log('Block received! ' + JSON.stringify(block));
});
$scope.getFooter = function() { $scope.getFooter = function() {
Status.get({ Status.get({
q: 'getInfo' q: 'getInfo'

View File

@ -6,15 +6,14 @@ angular.module('insight.system').controller('IndexController',
['$scope', ['$scope',
'$rootScope', '$rootScope',
'Global', 'Global',
'socket', 'get_socket',
'Blocks', 'Blocks',
'Transactions', 'Transactions',
function($scope, $rootScope, Global, socket, Blocks, Transactions) { function($scope, $rootScope, Global, get_socket, Blocks, Transactions) {
$scope.global = Global; $scope.global = Global;
socket.on('connect', function() { var socket = get_socket($scope);
socket.emit('subscribe', 'inv'); socket.emit('subscribe', 'inv');
});
//show errors //show errors
$scope.flashMessage = $rootScope.flashMessage || null; $scope.flashMessage = $rootScope.flashMessage || null;

View File

@ -2,14 +2,14 @@
angular.module('insight.transactions').controller('transactionsController', angular.module('insight.transactions').controller('transactionsController',
['$scope', ['$scope',
'$rootScope',
'$routeParams', '$routeParams',
'$location', '$location',
'Global', 'Global',
'Transaction', 'Transaction',
'TransactionsByBlock', 'TransactionsByBlock',
'TransactionsByAddress', 'TransactionsByAddress',
'socket', function ($scope, $rootScope, $routeParams, $location, Global, Transaction, TransactionsByBlock, TransactionsByAddress) {
function ($scope, $routeParams, $location, Global, Transaction, TransactionsByBlock, TransactionsByAddress, socket) {
$scope.global = Global; $scope.global = Global;
$scope.findThis = function() { $scope.findThis = function() {
@ -43,10 +43,10 @@ angular.module('insight.transactions').controller('transactionsController',
$scope.txs = txs; $scope.txs = txs;
}); });
}; };
socket.on('tx', function(tx) { /*socket.on('tx', function(tx) {
console.log('Incoming message for new transaction!', tx); console.log('Incoming message for new transaction!', tx);
$scope.findTx(tx.txid); $scope.findTx(tx.txid);
}); });*/
$scope.txs = []; $scope.txs = [];

View File

@ -1,26 +1,62 @@
'use strict'; 'use strict';
angular.module('insight.socket').factory('socket', ['$rootScope', function($rootScope) { var ScopedSocket = function(socket, $rootScope) {
var socket = io.connect(); this.socket = socket;
return { this.$rootScope = $rootScope;
on: function(eventName, callback) { this.listeners = [];
socket.on(eventName, function() { };
var args = arguments;
$rootScope.$apply(function() { ScopedSocket.prototype.removeAllListeners = function() {
callback.apply(socket, args); console.log('remove all listeners');
}); for (var i = 0; i < this.listeners.length; i++) {
}); var details = this.listeners[i];
}, console.log('removing listener '+i);
emit: function(eventName, data, callback) { this.socket.removeListener(details.event, details.fn);
socket.emit(eventName, data, function() { }
var args = arguments; this.listeners = [];
$rootScope.$apply(function() { };
if (callback) {
callback.apply(socket, args); ScopedSocket.prototype.on = function(event, callback) {
} var socket = this.socket;
}); var $rootScope = this.$rootScope;
});
}
}; var wrapped_callback = function() {
var args = arguments;
$rootScope.$apply(function() {
callback.apply(socket, args);
});
};
socket.on(event, wrapped_callback);
this.listeners.push({
event: event,
fn: wrapped_callback
});
};
ScopedSocket.prototype.emit = function(event, data, callback) {
var socket = this.socket;
var $rootScope = this.$rootScope;
socket.emit(event, data, function() {
var args = arguments;
$rootScope.$apply(function() {
if (callback) {
callback.apply(socket, args);
}
});
});
};
angular.module('insight.socket').factory('get_socket', ['$rootScope', function($rootScope) {
var socket = io.connect();
return function(scope) {
var scopedSocket = new ScopedSocket(socket, $rootScope);
scope.$on('$destroy', function() {
scopedSocket.removeAllListeners();
});
return scopedSocket;
};
}]); }]);