Skip to content
This repository was archived by the owner on Apr 29, 2020. It is now read-only.

Commit 0505652

Browse files
committed
Integrate libp2p-ping
1 parent 3ec7435 commit 0505652

File tree

3 files changed

+18
-1
lines changed

3 files changed

+18
-1
lines changed

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
"run-parallel": "^1.1.6"
4040
},
4141
"dependencies": {
42+
"libp2p-ping": "^0.2.0",
4243
"libp2p-secio": "^0.5.0",
4344
"libp2p-spdy": "^0.9.0",
4445
"libp2p-swarm": "^0.23.0",
@@ -67,4 +68,4 @@
6768
"Stephen Whitmore <stephen.whitmore@gmail.com>",
6869
"dignifiedquire <dignifiedquire@gmail.com>"
6970
]
70-
}
71+
}

src/index.js

+7
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ const secio = require('libp2p-secio')
99
const PeerId = require('peer-id')
1010
const PeerInfo = require('peer-info')
1111
const PeerBook = require('peer-book')
12+
const Ping = require('libp2p-ping')
1213
const multiaddr = require('multiaddr')
1314
const mafmt = require('mafmt')
1415
const EE = require('events').EventEmitter
@@ -197,6 +198,12 @@ exports.Node = function Node (pInfo, pBook) {
197198
return this.swarm.unhandle(protocol)
198199
}
199200

201+
Ping.attach(this.swarm) // Enable this peer to echo Ping requests
202+
203+
this.ping = (peerDst) => {
204+
return new Ping(this.swarm, peerDst) // Ping peerDst, peerDst must be a peer-info object
205+
}
206+
200207
this.discovery = new EE()
201208
this.routing = null
202209
this.records = null

test/libp2p.spec.js

+9
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,15 @@ describe('libp2p-ipfs', () => {
415415
}
416416
})
417417

418+
it('nodeE ping to nodeF', (done) => {
419+
const p = nodeE.ping(nodeF.peerInfo)
420+
421+
p.on('ping', (time) => {
422+
p.stop()
423+
done()
424+
})
425+
})
426+
418427
it('stop', (done) => {
419428
parallel([
420429
nodeA.stop,

0 commit comments

Comments
 (0)