added socket.io

This commit is contained in:
cubedro 2015-02-16 18:44:26 +02:00
parent 9cc41fca47
commit 6802561e45
3 changed files with 44 additions and 16 deletions

2
app.js
View File

@ -1,6 +1,6 @@
var nodeModel = require('./lib/node');
var node = new nodeModel(config);
var node = new nodeModel();
console.log(node.stats);

View File

@ -4,11 +4,16 @@ var os = require('os');
var slugs = require('slugs');
var HttpRequest = require('xmlhttprequest').XMLHttpRequest;
var io = require('socket.io-client');
var socket = io.connect('http://localhost:3000', {reconnect: true});
var MAX_BLOCKS_HISTORY = 12,
LOWEST_TIMESTAMP = 0;
function Node()
{
var self = this;
this.info = {
ip: getExternalIp(),
name: process.env.ETH_CLIENT,
@ -37,14 +42,31 @@ function Node()
errors: []
}
this.socket = false;
this.pendingWatch = false;
this.chainWatch = false;
this.updateInterval = false;
web3.setProvider(new web3.providers.HttpSyncProvider('http://' + (process.env.RPC_HOST || 'localhost') + ':' + (process.env.RPC_PORT || '8080')));
this.socket = require('socket.io-client')(process.env.SOCKET_SERVER || 'wss://localhost/socket.io/');
this.init();
// this.init();
socket.on('connect', function(data) {
console.log(data);
self.socket = true;
self.emit('hello', self.info);
console.log('connected!');
socket.on('disconnect', function(data) {
console.log(data);
self.socket = false;
console.log('disconnected');
})
});
return this;
}
@ -237,7 +259,7 @@ Node.prototype.update = function()
{
this.getStats();
this.socket.emit('update', this.prepareStats());
this.emit('update', this.prepareStats());
return this.stats;
};
@ -262,25 +284,31 @@ Node.prototype.setWatches = function()
}, 1000);
}
Node.prototype.emit = function(message, payload)
{
if(this.socket){
try {
socket.emit(message, payload);
return true
}
catch (err) {
console.log(err);
return false;
}
}
}
Node.prototype.init = function()
{
var self = this;
this.socket.on('connect', function(){
self.socket.emit('hello', self.info);
});
this.socket.on('disconnect', function() {
self.socket.emit('goodbye', { id: self.info.id });
})
this.update();
this.setWatches();
}
Node.prototype.stop = function()
{
this.socket.disconnect();
this.emit('disconnect');
if(this.updateInterval)
clearInterval(this.updateInterval);

View File

@ -27,7 +27,7 @@
"RPC_PORT" : "8080",
"ETH_CLIENT" : "",
"ETH_TYPE" : "C++ 0.8.1",
"SOCKET_SERVER" : "wss://eth-netstats.herokuapp.com/socket.io/",
"SOCKET_HOST" : "eth-netstats.herokuapp.com",
}
}
]