diff --git a/examples/SimpleP2Pmonitor.js b/examples/SimpleP2Pmonitor.js new file mode 100644 index 0000000..1791b9b --- /dev/null +++ b/examples/SimpleP2Pmonitor.js @@ -0,0 +1,72 @@ +/** + * This is a simple script that will display network messages. + * It users the Peer / Connection classes * directly instead of + * relying on PeerManager. + */ + +// replace by require('bitcore') if you use somewhere else +var bitcore = require('../'); + +//bitcore.config.logger = 'debug'; + +var Peer = bitcore.Peer, + Connection = bitcore.Connection; + +var peer = new Peer('127.0.0.1', 8333); + +var socket = peer.createConnection(); + +var con = new Connection(socket, peer); + +con.on('error', function (msg) { + var peer = msg.peer, err = msg.err; + console.error('Error connecting to peer', peer.host + ':' + peer.port, '(' + err.message + ')'); +}); + +con.on('disconnect', function (msg) { + console.log('disconnect: ', msg); +}); + +con.on('connect', function (msg) { + console.log('Connected to %s', msg.peer.host + ':' + msg.peer.port); +}); + +/* Listen P2P messages */ + +// Make a log function available to all listeners +// The log function is just like console.log except it prefixes +// messages with [host:port] +function listen (event_name, fn) { + con.on(event_name, function (event) { + fn(event, function () { + var args = Array.prototype.slice.call(arguments); + var str = args.shift(); + str = '[%s:%s] ' + str; + args = [ str, event.peer.host, event.peer.port ].concat(args); + console.log.apply(console, args); + }); + }); +} + +listen('getaddr', function (event, log) { + log('Received message getaddr'); + log(event); +}); + +listen('verack', function (event, log) { + log('Received message verack'); +}); + +listen('version', function (event, log) { + log('Received message version (%s)', event.message.version); +}); + +listen('addr', function (event, log) { + log('Received message addr (%s addresses)', event.message.addrs.length); +}); + +listen('inv', function (event, log) { + log('Received message inv (%s invs)', event.message.count); + console.log(event.message.invs); +}); + diff --git a/examples/example.html b/examples/browser/example.html similarity index 94% rename from examples/example.html rename to examples/browser/example.html index 74a1f64..b0914b3 100644 --- a/examples/example.html +++ b/examples/browser/example.html @@ -9,12 +9,12 @@
       
- + - + diff --git a/examples/simple.html b/examples/browser/simple.html similarity index 52% rename from examples/simple.html rename to examples/browser/simple.html index 740b3e6..18f35ef 100644 --- a/examples/simple.html +++ b/examples/browser/simple.html @@ -1,12 +1,13 @@ - + +