diff --git a/package.json b/package.json index bb5b411..4c76d4b 100644 --- a/package.json +++ b/package.json @@ -45,6 +45,7 @@ }, "dependencies": { "babel-runtime": "^6.11.6", + "libp2p-ping": "^0.2.0", "libp2p-secio": "^0.5.0", "libp2p-spdy": "^0.9.0", "libp2p-swarm": "^0.23.0", @@ -62,4 +63,4 @@ "Richard Littauer <richard.littauer@gmail.com>", "greenkeeperio-bot <support@greenkeeper.io>" ] -} \ No newline at end of file +} diff --git a/src/index.js b/src/index.js index 514c328..5ead076 100644 --- a/src/index.js +++ b/src/index.js @@ -11,6 +11,7 @@ const EE = require('events').EventEmitter const multiaddr = require('multiaddr') const PeerBook = require('peer-book') const mafmt = require('mafmt') +const Ping = require('libp2p-ping') exports = module.exports @@ -200,6 +201,12 @@ exports.Node = function Node (pInfo, pBook) { return this.swarm.unhandle(protocol) } + Ping.attach(this.swarm) // Enable this peer to echo Ping requests + + this.ping = (peerDst) => { + return new Ping(this.swarm, peerDst) // Ping peerDst, peerDst must be a peer-info object + } + this.discovery = new EE() this.routing = null this.records = null diff --git a/test/webrtc-star-only.js b/test/webrtc-star-only.js index f3e86f7..20cdeaa 100644 --- a/test/webrtc-star-only.js +++ b/test/webrtc-star-only.js @@ -116,4 +116,13 @@ describe('libp2p-ipfs-browser (webrtc only)', function () { }, 2000) }) }) + + it('node1 ping to node2', (done) => { + const p = node1.ping(node2.peerInfo) + + p.once('ping', (time) => { + p.stop() + done() + }) + }) })