bitcore/examples/PeerManager.js

48 lines
956 B
JavaScript
Raw Normal View History

2014-02-19 11:48:52 -08:00
'use strict';
2014-02-24 04:06:54 -08:00
// Replace '..' with 'bitcore' if you plan on using this code elsewhere.
var util = require('util');
var networks = require('../networks');
2014-03-05 11:18:44 -08:00
var Peer = require('../Peer');
var PeerManager = require('soop').load('../PeerManager',
{network: networks.testnet});
2014-02-24 04:06:54 -08:00
var handleBlock = function(info) {
2014-02-19 11:48:52 -08:00
2014-02-24 04:06:54 -08:00
console.log('** Block Received **');
console.log(info.message);
};
var handleTx = function(info) {
2014-02-24 04:06:54 -08:00
var tx = info.message.tx.getStandardizedObject();
2014-02-24 04:06:54 -08:00
console.log('** Block TX **');
console.log(tx);
2014-02-19 11:48:52 -08:00
};
var handleInv = function(info) {
2014-02-24 04:06:54 -08:00
console.log('** Block Inv **');
console.log(info.message);
var invs = info.message.invs;
info.conn.sendGetData(invs);
2014-02-24 04:06:54 -08:00
};
2014-02-19 11:48:52 -08:00
var peerman = new PeerManager();
2014-02-24 04:06:54 -08:00
peerman.addPeer( new Peer('127.0.0.1', 18333) );
2014-02-19 11:48:52 -08:00
peerman.on('connection', function(conn) {
2014-02-24 04:06:54 -08:00
conn.on('inv', handleInv);
conn.on('block', handleBlock);
2014-02-24 04:06:54 -08:00
conn.on('tx', handleTx);
});
2014-02-24 04:06:54 -08:00
peerman.start();