added socket functionality

This commit is contained in:
cubedro 2015-02-17 07:12:44 +02:00
parent 8954fbae16
commit dd0dcd00da
9 changed files with 156 additions and 130 deletions

116
app.js
View File

@ -1,20 +1,18 @@
var express = require('express.io');
var express = require('express');
var app = express();
var path = require('path');
var favicon = require('serve-favicon');
var bodyParser = require('body-parser');
var Node = require('./models/node');
var Primus = require('primus'),
http = require('http'),
server,
api,
client;
var Collection = require('./models/collection');
var Nodes = new Collection();
server = http.createServer();
var server = require('http').createServer(app);
api = new Primus(server, {
transformer: 'websockets',
pathname: '/api',
@ -23,23 +21,6 @@ api = new Primus(server, {
api.use('emit', require('primus-emit'));
api.on('connection', function(spark) {
console.log(spark.id);
console.log(spark.address);
console.log(spark.query);
spark.on('hello', function(data){
console.log('got hello data from ' + spark.id);
console.log(data);
Nodes.add(data);
});
spark.on('update', function(data){
console.log('got update from ' + spark.id);
console.log(data);
});
});
var client = new Primus(server, {
transformer: 'websockets',
pathname: '/primus',
@ -48,65 +29,63 @@ var client = new Primus(server, {
client.use('emit', require('primus-emit'));
client.on('connection', function(spark) {
console.log(spark);
api.on('connection', function(spark) {
console.log(spark.id);
console.log(spark.headers);
console.log(spark.address);
console.log(spark.query);
spark.write({give: 'identity'});
spark.on('hello', function(data){
console.log('got hello data from ' + spark.id);
console.log(data);
if(typeof data.id !== 'undefined' && typeof data.info !== 'undefined')
{
data.ip = spark.address.ip;
var info = Nodes.add(data);
spark.emit('ready');
client.emit('new', info);
}
});
spark.on('update', function(data){
console.log('got update from ' + spark.id);
console.log(data);
if(typeof data.id !== 'undefined' && typeof data.stats !== 'undefined')
{
var stats = Nodes.update(data.id, data.stats);
client.emit('updated', stats);
}
});
});
server.listen(process.env.API_PORT || 3000);
client.on('connection', function(spark) {
console.log(spark.id);
console.log(spark.address);
console.log(spark.query);
// if(fs.existsSync('./config/nodes.js')){
// config = require('./config/nodes');
// } else {
// config = require('./config/nodes.default');
// }
spark.on('ready', function(data){
console.log('got hello data from ' + spark.id);
console.log(data);
// var Node = require('./models/node');
var app = express();
app.http().io();
spark.emit('init', {nodes: Nodes.all()});
});
});
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'jade');
// uncomment after placing favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(express.static(path.join(__dirname, 'public')));
// var nodes = [],
// nodeStatus = [],
// nodeInterval;
// for(i in config) {
// nodes[i] = new Node(config[i], i);
// console.log(nodes[i]);
// nodeStatus[i] = nodes[i].update();
// }
// nodeInterval = setInterval(function(){
// for(i in nodes){
// app.io.broadcast('update', nodes[i].update());
// }
// }, 10000);
// app.get('/', function(req, res) {
// res.render('index', { title: 'Ethereum Network Status' });
// });
// app.io.route('ready', function(req) {
// req.io.emit('init', {
// nodes: nodeStatus
// });
// console.log('emited');
// });
app.get('/', function(req, res) {
res.render('index', { title: 'Ethereum Network Status' });
});
// catch 404 and forward to error handler
app.use(function(req, res, next) {
@ -116,9 +95,6 @@ app.use(function(req, res, next) {
});
// error handlers
// development error handler
// will print stacktrace
if (app.get('env') === 'development') {
app.use(function(err, req, res, next) {
res.status(err.status || 500);
@ -130,7 +106,6 @@ if (app.get('env') === 'development') {
}
// production error handler
// no stacktraces leaked to user
app.use(function(err, req, res, next) {
res.status(err.status || 500);
res.render('error', {
@ -139,5 +114,6 @@ app.use(function(err, req, res, next) {
});
});
server.listen(process.env.PORT || 3000);
module.exports = app;

View File

@ -2,8 +2,8 @@
var debug = require('debug')('eth-netstats');
var app = require('../app');
app.set('port', process.env.PORT || 3000);
// app.set('port', process.env.PORT || 3000);
var server = app.listen(app.get('port'), function() {
debug('Express server listening on port ' + server.address().port);
});
// var server = app.listen(app.get('port'), function() {
// debug('Express server listening on port ' + server.address().port);
// });

View File

@ -10,44 +10,63 @@ var Collection = function Collection()
Collection.prototype.add = function(data)
{
if(typeof data.id == 'undefined')
return false;
var node = this.getNodeOrNew({ id : data.id }, data);
var node = this.getNodeOrNew({ id : data.id });
node.info = data.info;
return node.getInfo();
}
Collection.prototype.get = function(id)
Collection.prototype.update = function(id, stats)
{
return this.getNode(id);
var node = this.getNode({ id: id });
if(!node)
return false;
node.stats = stats;
console.log(this.all());
return node.getStats();
}
Collection.prototype.getIndex = function(search)
{
return _.findIndex(this._list, search);
return (index >= 0 ? index : false);
}
Collection.prototype.getNode = function(search)
{
var index = this.getIndex(search);
if(index)
if(index >= 0)
return this._list[index];
return false;
}
Collection.prototype.getIndexOrNew = function(search)
Collection.prototype.getNodeByIndex = function(index)
{
var index = this.getIndex(search) || this._list.push(new Node());
if(this._list[index])
return this._list[index];
return false;
}
Collection.prototype.getNodeOrNew = function(search)
Collection.prototype.getIndexOrNew = function(search, data)
{
return this.getNode(this.getIndexOrNew(search));
var index = this.getIndex(search);
return (index >= 0 ? index : this._list.push(new Node(data)) - 1);
}
Collection.prototype.getNodeOrNew = function(search, data)
{
return this.getNodeByIndex(this.getIndexOrNew(search, data));
}
Collection.prototype.all = function()
{
return this._list;
}
module.exports = Collection;

View File

@ -1,6 +1,6 @@
var geoip = require('geoip-lite');
var Node = function Node()
var Node = function Node(data)
{
this.id = null;
this.info = {};
@ -15,20 +15,35 @@ var Node = function Node()
block: {},
blocktimeAvg: 0,
difficulty: [],
uptime: {
down: 0,
inc: 0,
total: 0
},
uptime: 0,
lastUpdate: 0
};
if(typeof data.id !== 'undefined')
this.id = data.id;
if(typeof data.info !== 'undefined')
this.info = data.info;
if(typeof data.ip !== 'undefined')
this.setGeo(data.ip);
return this;
}
Node.prototype.setGeo = function()
Node.prototype.setGeo = function(ip)
{
this.geo = geoip.lookup(ip);
}
Node.prototype.getInfo = function()
{
return {id: this.id, info: this.info, geo: this.geo};
}
Node.prototype.getStats = function()
{
return {id: this.id, stats: this.stats};
}
module.exports = Node;

View File

@ -18,10 +18,27 @@ function StatsCtrl($scope, $filter, socket, _) {
// Socket listeners
// ----------------
socket.emit('ready');
socket = new Primus();
socket.on('open', function open() {
socket.emit('ready');
console.log('The connection has been opened.');
}).on('end', function end() {
self._socket = false;
}).on('error', function error(err) {
console.log(err);
}).on('reconnecting', function reconnecting(opts) {
console.log('We are scheduling a reconnect operation', opts);
}).on('data', function incoming(data) {
console.log('Received some data', data);
});
// socket.emit('ready', 'test');
socket.on('init', function(data)
{
console.log(data);
$scope.nodes = data.nodes;
updateStats();
@ -29,6 +46,8 @@ function StatsCtrl($scope, $filter, socket, _) {
socket.on('update', function(data)
{
console.log(data);
$scope.nodes[data.id] = data;
updateStats();
@ -51,7 +70,7 @@ function StatsCtrl($scope, $filter, socket, _) {
}).stats.block.timestamp;
$scope.upTimeTotal = _.reduce($scope.nodes, function(total, node) {
return total + node.stats.uptime.total;
return total + node.stats.uptime;
}, 0) / $scope.nodes.length;
$scope.map = _.map($scope.nodes, function(node) {
@ -69,5 +88,7 @@ function StatsCtrl($scope, $filter, socket, _) {
longitude: 0
};
});
$scope.$apply();
}
}

View File

@ -1,10 +1,4 @@
(function() {
var socket = io.connect();
socket.on('init', function(data){
console.log(data);
});
$('body').on('mouseenter', '[data-toggle="tooltip"]', function( event ) {
$(this).tooltip('show');
}).on('mouseleave', '[data-toggle="tooltip"]', function( event ) {

View File

@ -3,27 +3,28 @@
/* Services */
app.factory('socket', function ($rootScope) {
var socket = io.connect();
return {
on: function (eventName, callback) {
socket.on(eventName, function () {
var args = arguments;
$rootScope.$apply(function () {
callback.apply(socket, args);
});
});
},
emit: function (eventName, data, callback) {
socket.emit(eventName, data, function () {
var args = arguments;
$rootScope.$apply(function () {
if (callback) {
callback.apply(socket, args);
}
});
})
}
};
var socket;// = new Primus();
return socket;
// return {
// on: function (eventName, callback) {
// socket.on(eventName, function () {
// var args = arguments;
// $rootScope.$apply(function () {
// callback.apply(socket, args);
// });
// });
// },
// emit: function (eventName, data, callback) {
// socket.emit(eventName, data, function () {
// var args = arguments;
// $rootScope.$apply(function () {
// if (callback) {
// callback.apply(socket, args);
// }
// });
// })
// }
// };
});
var underscore = angular.module('underscore', []);

View File

@ -75,10 +75,10 @@ block content
tbody
tr(ng-repeat='node in nodes', class="{{ node.stats | mainClass : bestBlock }}")
td(rel="{{node.id}}")
span(data-toggle="tooltip", data-placement="top", data-original-title="{{node.geo | geoTooltip}}") {{node.name}}
div.small {{node.ip}}
td {{node.type}}
div.small {{node.os}}
span(data-toggle="tooltip", data-placement="top", data-original-title="{{node.geo | geoTooltip}}") {{node.info.name}}
div.small {{node.info.ip}}
td {{node.info.node}}
div.small {{node.info.os}}, {{node.info.os_v}}
td(class="{{ node.stats.peers | peerClass }}") {{node.stats.peers}}
td(class="{{ node.stats.mining | miningClass }}")
i(class="{{ node.stats.mining | miningIconClass }}")
@ -87,4 +87,4 @@ block content
span.small {{node.stats.block.hash}}
div.small Difficulty: {{node.stats.block.difficulty | gasFilter}} | Gas used: {{node.stats.block.gasUsed | gasFilter}} | Min gas price: {{node.stats.block.minGasPrice | gasFilter}} | Gas limit: {{node.stats.block.gasLimit | gasFilter}}
td(am-time-ago="node.stats.block.timestamp", am-preprocess="unix", class="{{ node.stats.block.timestamp | timeClass }}") ∞
td(class="{{ node.stats.uptime.total | upTimeClass }}") {{ node.stats.uptime.total | upTimeFilter }}
td(class="{{ node.stats.uptime | upTimeClass }}") {{ node.stats.uptime | upTimeFilter }}

View File

@ -12,7 +12,7 @@ html(ng-app="netStatsApp")
block content
script(src="/js/lib/angular.min.js")
script(src="/socket.io/socket.io.js")
script(src="/primus/primus.js")
script(src="/js/lib/underscore.min.js")
script(src="/js/lib/jquery.min.js")
script(src="/js/lib/d3.v3.min.js")