Start a node and expose API methods and events over a socket.
This commit is contained in:
parent
6764871a95
commit
598cf64a5f
78
bin/start-node.js
Normal file
78
bin/start-node.js
Normal file
@ -0,0 +1,78 @@
|
||||
'use strict';
|
||||
|
||||
var BitcoinNode = require('..').Node;
|
||||
var chainlib = require('chainlib');
|
||||
var io = require('socket.io');
|
||||
var log = chainlib.log;
|
||||
log.debug = function() {};
|
||||
|
||||
var configuration = {
|
||||
datadir: process.env.BITCOINDJS_DIR || '~/.bitcoin',
|
||||
network: process.env.BITCOINDJS_NETWORK || 'livenet'
|
||||
};
|
||||
|
||||
var node = new BitcoinNode(configuration);
|
||||
|
||||
var count = 0;
|
||||
var interval;
|
||||
|
||||
node.on('ready', function() {
|
||||
|
||||
interval = setInterval(function() {
|
||||
log.info('Sync Status: Tip:', node.chain.tip.hash, 'Height:', node.chain.tip.__height, 'Rate:', count/10, 'blocks per second');
|
||||
count = 0;
|
||||
}, 10000);
|
||||
|
||||
io.on('connection', function(socket) {
|
||||
|
||||
var bus = node.openBus();
|
||||
|
||||
var methods = node.getAllAPIMethods();
|
||||
var methodsMap = {};
|
||||
|
||||
methods.forEach(function(data) {
|
||||
var name = data[0];
|
||||
var instance = data[1];
|
||||
var method = data[2];
|
||||
methodsMap[name] = function() {
|
||||
return method.apply(instance, arguments);
|
||||
};
|
||||
});
|
||||
|
||||
socket.on('message', function(message) {
|
||||
if (methodsMap[message.command]) {
|
||||
methodsMap[message.command](message.params);
|
||||
}
|
||||
});
|
||||
|
||||
socket.on('subscribe', function(name, params) {
|
||||
bus.subscribe(name, params);
|
||||
});
|
||||
|
||||
socket.on('unsubscribe', function(name, params) {
|
||||
bus.unsubscribe(name, params);
|
||||
});
|
||||
|
||||
var events = node.getAllPublishEvents();
|
||||
|
||||
events.forEach(function(event) {
|
||||
bus.on(event.name, function(data) {
|
||||
socket.emit(event.name, data);
|
||||
});
|
||||
});
|
||||
|
||||
socket.on('disconnect', function() {
|
||||
bus.close();
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
});
|
||||
|
||||
node.on('error', function(err) {
|
||||
log.error(err);
|
||||
});
|
||||
|
||||
node.chain.on('addblock', function(block) {
|
||||
count++;
|
||||
});
|
||||
18
lib/node.js
18
lib/node.js
@ -29,6 +29,24 @@ Node.prototype.openBus = function() {
|
||||
return new Bus({db: this.db});
|
||||
};
|
||||
|
||||
Node.prototype.getAllAPIMethods = function() {
|
||||
var methods = [];
|
||||
for (var i = 0; i < this.db.modules.length; i++) {
|
||||
var mod = this.db.modules[i];
|
||||
methods = methods.concat(mod.getAPIMethods());
|
||||
}
|
||||
return methods;
|
||||
};
|
||||
|
||||
Node.prototype.getAllPublishEvents = function() {
|
||||
var events = [];
|
||||
for (var i = 0; i < this.db.modules.length; i++) {
|
||||
var mod = this.db.modules[i];
|
||||
events = events.concat(mod.getPublishEvents());
|
||||
}
|
||||
return events;
|
||||
};
|
||||
|
||||
Node.prototype._loadConfiguration = function(config) {
|
||||
var self = this;
|
||||
this._loadBitcoinConf(config);
|
||||
|
||||
@ -29,7 +29,7 @@
|
||||
"scripts": {
|
||||
"preinstall": "./bin/build-libbitcoind",
|
||||
"install": "./bin/build-bindings",
|
||||
"start": "node example",
|
||||
"start": "node bin/start.js",
|
||||
"test": "NODE_ENV=test mocha --recursive",
|
||||
"coverage": "istanbul cover _mocha -- --recursive",
|
||||
"libbitcoind": "node bin/start-libbitcoind.js"
|
||||
@ -45,7 +45,9 @@
|
||||
"errno": "^0.1.2",
|
||||
"memdown": "^1.0.0",
|
||||
"mkdirp": "0.5.0",
|
||||
"nan": "1.3.0"
|
||||
"nan": "1.3.0",
|
||||
"socket.io": "^1.3.6",
|
||||
"tiny": "0.0.10"
|
||||
},
|
||||
"devDependencies": {
|
||||
"benchmark": "1.0.0",
|
||||
|
||||
Loading…
Reference in New Issue
Block a user