added socket connectivity

This commit is contained in:
cubedro 2015-02-12 16:02:44 +02:00
parent b87f19dc58
commit c500f4e069
3 changed files with 33 additions and 10 deletions

View File

@ -1,7 +1,7 @@
var config = { var config = {
name: 'Node', name: 'Node',
type: 'C++', type: 'C++',
serverHost: 'localhost', serverHost: 'http://localhost',
serverPort: '3000' serverPort: '3000'
}; };

View File

@ -2,7 +2,7 @@ var web3 = require('ethereum.js');
var _ = require('underscore'); var _ = require('underscore');
var os = require('os'); var os = require('os');
var slugs = require('slugs'); var slugs = require('slugs');
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest; var HttpRequest = require('xmlhttprequest').XMLHttpRequest;
var MAX_BLOCKS_HISTORY = 12, var MAX_BLOCKS_HISTORY = 12,
LOWEST_TIMESTAMP = 0; LOWEST_TIMESTAMP = 0;
@ -11,7 +11,7 @@ function Node(options)
{ {
this.options = options; this.options = options;
this.info = { this.info = {
ip: this.getExternalIp(), ip: getExternalIp(),
name: options.name, name: options.name,
type: options.type, type: options.type,
os: os.platform(), os: os.platform(),
@ -42,17 +42,17 @@ function Node(options)
this.chainWatch = false; this.chainWatch = false;
this.updateInterval = false; this.updateInterval = false;
var sock = new web3.providers.HttpSyncProvider('http://' + (process.env.RPC_HOST || 'localhost') + ':' + (process.env.RPC_PORT || '8080')); web3.setProvider(new web3.providers.HttpSyncProvider('http://' + (process.env.RPC_HOST || 'localhost') + ':' + (process.env.RPC_PORT || '8080')));
web3.setProvider(sock); this.socket = require('socket.io-client')((process.env.SOCKET_SERVER || options.serverHost) + ':' + (process.env.SOCKET_PORT || options.serverPort));
this.init(); this.init();
return this; return this;
} }
Node.prototype.getExternalIp = function() function getExternalIp()
{ {
var request = new XMLHttpRequest(); var request = new HttpRequest();
request.open('GET', 'http://curlmyip.com/', false); request.open('GET', 'http://curlmyip.com/', false);
request.send(); request.send();
@ -226,10 +226,20 @@ Node.prototype.getStats = function()
this.uptime(); this.uptime();
} }
Node.prototype.prepareStats = function()
{
return {
id: this.info.id,
stats: this.stats
};
}
Node.prototype.update = function() Node.prototype.update = function()
{ {
this.getStats(); this.getStats();
this.socket.emit('update', this.prepareStats());
return this.stats; return this.stats;
}; };
@ -239,7 +249,7 @@ Node.prototype.setWatches = function()
this.pendingWatch = web3.eth.watch('pending'); this.pendingWatch = web3.eth.watch('pending');
this.pendingWatch.changed(function(log) { this.pendingWatch.changed(function(log) {
console.log('pending changed'); console.log('pending changed');
self.stats.pending = parseInt(log.number); self.stats.pending = parseInt(log.length);
}); });
this.chainWatch = web3.eth.watch('chain'); this.chainWatch = web3.eth.watch('chain');
@ -255,12 +265,24 @@ Node.prototype.setWatches = function()
Node.prototype.init = function() 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.update();
this.setWatches(); this.setWatches();
} }
Node.prototype.stop = function() Node.prototype.stop = function()
{ {
this.socket.disconnect();
if(this.updateInterval) if(this.updateInterval)
clearInterval(this.updateInterval); clearInterval(this.updateInterval);

View File

@ -9,8 +9,9 @@
"debug": "~2.0.0", "debug": "~2.0.0",
"ethereum.js": "*", "ethereum.js": "*",
"express.io": "^1.1.13", "express.io": "^1.1.13",
"xmlhttprequest": "*",
"slugs": "^0.1.3", "slugs": "^0.1.3",
"underscore": "^1.7.0" "socket.io-client": "^1.3.3",
"underscore": "^1.7.0",
"xmlhttprequest": "*"
} }
} }