From c500f4e0698036e4c06beeb17183351460cd5bd7 Mon Sep 17 00:00:00 2001 From: cubedro Date: Thu, 12 Feb 2015 16:02:44 +0200 Subject: [PATCH] added socket connectivity --- config.default.js | 2 +- lib/node.js | 36 +++++++++++++++++++++++++++++------- package.json | 5 +++-- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/config.default.js b/config.default.js index ca38121..50121b3 100644 --- a/config.default.js +++ b/config.default.js @@ -1,7 +1,7 @@ var config = { name: 'Node', type: 'C++', - serverHost: 'localhost', + serverHost: 'http://localhost', serverPort: '3000' }; diff --git a/lib/node.js b/lib/node.js index 8512984..18dd32f 100644 --- a/lib/node.js +++ b/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); diff --git a/package.json b/package.json index fb47481..df8920e 100644 --- a/package.json +++ b/package.json @@ -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": "*" } }