added socket connectivity
This commit is contained in:
parent
b87f19dc58
commit
c500f4e069
@ -1,7 +1,7 @@
|
||||
var config = {
|
||||
name: 'Node',
|
||||
type: 'C++',
|
||||
serverHost: 'localhost',
|
||||
serverHost: 'http://localhost',
|
||||
serverPort: '3000'
|
||||
};
|
||||
|
||||
|
36
lib/node.js
36
lib/node.js
@ -2,7 +2,7 @@ var web3 = require('ethereum.js');
|
||||
var _ = require('underscore');
|
||||
var os = require('os');
|
||||
var slugs = require('slugs');
|
||||
var XMLHttpRequest = require('xmlhttprequest').XMLHttpRequest;
|
||||
var HttpRequest = require('xmlhttprequest').XMLHttpRequest;
|
||||
|
||||
var MAX_BLOCKS_HISTORY = 12,
|
||||
LOWEST_TIMESTAMP = 0;
|
||||
@ -11,7 +11,7 @@ function Node(options)
|
||||
{
|
||||
this.options = options;
|
||||
this.info = {
|
||||
ip: this.getExternalIp(),
|
||||
ip: getExternalIp(),
|
||||
name: options.name,
|
||||
type: options.type,
|
||||
os: os.platform(),
|
||||
@ -42,17 +42,17 @@ function Node(options)
|
||||
this.chainWatch = false;
|
||||
this.updateInterval = false;
|
||||
|
||||
var sock = new web3.providers.HttpSyncProvider('http://' + (process.env.RPC_HOST || 'localhost') + ':' + (process.env.RPC_PORT || '8080'));
|
||||
web3.setProvider(sock);
|
||||
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 || options.serverHost) + ':' + (process.env.SOCKET_PORT || options.serverPort));
|
||||
|
||||
this.init();
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
Node.prototype.getExternalIp = function()
|
||||
function getExternalIp()
|
||||
{
|
||||
var request = new XMLHttpRequest();
|
||||
var request = new HttpRequest();
|
||||
request.open('GET', 'http://curlmyip.com/', false);
|
||||
request.send();
|
||||
|
||||
@ -226,10 +226,20 @@ Node.prototype.getStats = function()
|
||||
this.uptime();
|
||||
}
|
||||
|
||||
Node.prototype.prepareStats = function()
|
||||
{
|
||||
return {
|
||||
id: this.info.id,
|
||||
stats: this.stats
|
||||
};
|
||||
}
|
||||
|
||||
Node.prototype.update = function()
|
||||
{
|
||||
this.getStats();
|
||||
|
||||
this.socket.emit('update', this.prepareStats());
|
||||
|
||||
return this.stats;
|
||||
};
|
||||
|
||||
@ -239,7 +249,7 @@ Node.prototype.setWatches = function()
|
||||
this.pendingWatch = web3.eth.watch('pending');
|
||||
this.pendingWatch.changed(function(log) {
|
||||
console.log('pending changed');
|
||||
self.stats.pending = parseInt(log.number);
|
||||
self.stats.pending = parseInt(log.length);
|
||||
});
|
||||
|
||||
this.chainWatch = web3.eth.watch('chain');
|
||||
@ -255,12 +265,24 @@ Node.prototype.setWatches = 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.setWatches();
|
||||
}
|
||||
|
||||
Node.prototype.stop = function()
|
||||
{
|
||||
this.socket.disconnect();
|
||||
|
||||
if(this.updateInterval)
|
||||
clearInterval(this.updateInterval);
|
||||
|
||||
|
@ -9,8 +9,9 @@
|
||||
"debug": "~2.0.0",
|
||||
"ethereum.js": "*",
|
||||
"express.io": "^1.1.13",
|
||||
"xmlhttprequest": "*",
|
||||
"slugs": "^0.1.3",
|
||||
"underscore": "^1.7.0"
|
||||
"socket.io-client": "^1.3.3",
|
||||
"underscore": "^1.7.0",
|
||||
"xmlhttprequest": "*"
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user