From 6802561e456ba0942c6ea8a4d55c984b6e274106 Mon Sep 17 00:00:00 2001
From: cubedro <contact@siteshop.ro>
Date: Mon, 16 Feb 2015 18:44:26 +0200
Subject: [PATCH] added socket.io

---
 app.js         |  2 +-
 lib/node.js    | 56 +++++++++++++++++++++++++++++++++++++-------------
 processes.json |  2 +-
 3 files changed, 44 insertions(+), 16 deletions(-)

diff --git a/app.js b/app.js
index 0c2db82..0939e74 100644
--- a/app.js
+++ b/app.js
@@ -1,6 +1,6 @@
 var nodeModel = require('./lib/node');
 
-var node = new nodeModel(config);
+var node = new nodeModel();
 
 console.log(node.stats);
 
diff --git a/lib/node.js b/lib/node.js
index 9ff9e13..20a780a 100644
--- a/lib/node.js
+++ b/lib/node.js
@@ -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);
diff --git a/processes.json b/processes.json
index 49bb586..9ff3eab 100644
--- a/processes.json
+++ b/processes.json
@@ -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",
 		}
 	}
 ]
\ No newline at end of file