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 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 Primus = require('primus'),
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,
LOWEST_TIMESTAMP = 0;
@ -15,9 +23,8 @@ function Node()
var self = this;
this.info = {
ip: getExternalIp(),
name: process.env.ETH_CLIENT,
type: process.env.ETH_TYPE,
name: process.env.ETH_CLIENT || 'C++',
type: process.env.ETH_TYPE || '0.8.1',
os: os.platform(),
os_v: os.release()
};
@ -42,7 +49,7 @@ function Node()
errors: []
}
this.socket = false;
this.socket = null;
this.pendingWatch = false;
this.chainWatch = false;
this.updateInterval = false;
@ -51,41 +58,26 @@ function Node()
// this.init();
socket.on('connect', function(data) {
console.log(data);
socket.on('open', function open() {
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');
})
console.log('The connection has been opened.');
}).on('end', function end() {
self.socket = false;
}).on('error', function error(err) {
console.log(err);
}).on('reconnecting', function reconnecting(opts) {
console.log('We are scheduling a reconnect operation', opts);
}).on('data', function incoming(data) {
console.log('Received some data', data);
});
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()
{
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()
@ -289,13 +281,9 @@ Node.prototype.emit = function(message, payload)
if(this.socket){
try {
socket.emit(message, payload);
return true
}
catch (err) {
console.log(err);
return false;
}
}
}
@ -308,7 +296,8 @@ Node.prototype.init = function()
Node.prototype.stop = function()
{
this.emit('disconnect');
if(this.socket)
socket.end(socket.id);
if(this.updateInterval)
clearInterval(this.updateInterval);

View File

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