added primus socket functionality

This commit is contained in:
cubedro 2015-02-16 23:30:21 +02:00
parent 6802561e45
commit 024af1ebfb
2 changed files with 30 additions and 40 deletions

View File

@ -2,10 +2,18 @@ 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 HttpRequest = require('xmlhttprequest').XMLHttpRequest;
var io = require('socket.io-client'); var Primus = require('primus'),
var socket = io.connect('http://localhost:3000', {reconnect: true}); Emitter = require('primus-emit'),
Socket;
Socket = Primus.createSocket({
transformer: 'websockets',
pathname: '/api',
plugin: {emitter: Emitter}
});
var socket = new Socket(process.env.WS_SERVER || 'ws://localhost:3000');
var MAX_BLOCKS_HISTORY = 12, var MAX_BLOCKS_HISTORY = 12,
LOWEST_TIMESTAMP = 0; LOWEST_TIMESTAMP = 0;
@ -15,9 +23,8 @@ function Node()
var self = this; var self = this;
this.info = { this.info = {
ip: getExternalIp(), name: process.env.ETH_CLIENT || 'C++',
name: process.env.ETH_CLIENT, type: process.env.ETH_TYPE || '0.8.1',
type: process.env.ETH_TYPE,
os: os.platform(), os: os.platform(),
os_v: os.release() os_v: os.release()
}; };
@ -42,7 +49,7 @@ function Node()
errors: [] errors: []
} }
this.socket = false; this.socket = null;
this.pendingWatch = false; this.pendingWatch = false;
this.chainWatch = false; this.chainWatch = false;
this.updateInterval = false; this.updateInterval = false;
@ -51,41 +58,26 @@ function Node()
// this.init(); // this.init();
socket.on('connect', function(data) { socket.on('open', function open() {
console.log(data);
self.socket = true; self.socket = true;
self.emit('hello', self.info); self.emit('hello', self.info);
console.log('The connection has been opened.');
console.log('connected!'); }).on('end', function end() {
self.socket = false;
socket.on('disconnect', function(data) { }).on('error', function error(err) {
console.log(data); console.log(err);
}).on('reconnecting', function reconnecting(opts) {
self.socket = false; console.log('We are scheduling a reconnect operation', opts);
}).on('data', function incoming(data) {
console.log('disconnected'); console.log('Received some data', data);
})
}); });
return this; return this;
} }
function getExternalIp()
{
var request = new HttpRequest();
request.open('GET', 'http://curlmyip.com/', false);
request.send();
if(request.status !== 200)
return 'unknown';
return request.responseText.trim();
}
Node.prototype.makeId = function() Node.prototype.makeId = function()
{ {
return slugs(this.info.name + ' ' + this.info.type + ' ' + this.info.os + ' ' + this.info.os_v + ' ' + this.info.ip); return slugs(this.info.name + ' ' + this.info.type + ' ' + this.info.os + ' ' + this.info.os_v);
} }
Node.prototype.isActive = function() Node.prototype.isActive = function()
@ -289,13 +281,9 @@ Node.prototype.emit = function(message, payload)
if(this.socket){ if(this.socket){
try { try {
socket.emit(message, payload); socket.emit(message, payload);
return true
} }
catch (err) { catch (err) {
console.log(err); console.log(err);
return false;
} }
} }
} }
@ -308,7 +296,8 @@ Node.prototype.init = function()
Node.prototype.stop = function() Node.prototype.stop = function()
{ {
this.emit('disconnect'); if(this.socket)
socket.end(socket.id);
if(this.updateInterval) if(this.updateInterval)
clearInterval(this.updateInterval); clearInterval(this.updateInterval);

View File

@ -10,10 +10,11 @@
"dependencies": { "dependencies": {
"debug": "~2.0.0", "debug": "~2.0.0",
"ethereum.js": "*", "ethereum.js": "*",
"primus": "^2.4.12",
"primus-emit": "^0.1.2",
"slugs": "^0.1.3", "slugs": "^0.1.3",
"socket.io-client": "^1.3.3",
"underscore": "^1.7.0", "underscore": "^1.7.0",
"xmlhttprequest": "*" "ws": "^0.7.1"
}, },
"scripts": { "scripts": {
"start": "node ./app.js" "start": "node ./app.js"