mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-08-01 00:41:57 +00:00
feat: add .listen method
This commit is contained in:
@@ -102,6 +102,10 @@ dial uses the best transport (whatever works first, in the future we can have so
|
||||
- `protocol`
|
||||
- `callback`
|
||||
|
||||
### `swarm.listen(callback)`
|
||||
|
||||
Start listening on all added transports that are available on the current `peerInfo`.
|
||||
|
||||
### `swarm.handle(protocol, handler)`
|
||||
|
||||
handle a new protocol.
|
||||
|
@@ -79,4 +79,4 @@
|
||||
"Pau Ramon Revilla <masylum@gmail.com>",
|
||||
"Richard Littauer <richard.littauer@gmail.com>"
|
||||
]
|
||||
}
|
||||
}
|
||||
|
18
src/index.js
18
src/index.js
@@ -217,6 +217,14 @@ function Swarm (peerInfo) {
|
||||
msS.handle(conn)
|
||||
}
|
||||
|
||||
function availableTransports (pi) {
|
||||
const addrs = pi.multiaddrs
|
||||
return Object.keys(self.transports).filter((ts) => {
|
||||
// Only listen on transports we actually have addresses for
|
||||
return self.transports[ts].filter(addrs).length > 0
|
||||
})
|
||||
}
|
||||
|
||||
// higher level (public) API
|
||||
this.dial = (pi, protocol, callback) => {
|
||||
if (typeof protocol === 'function') {
|
||||
@@ -279,7 +287,7 @@ function Swarm (peerInfo) {
|
||||
}
|
||||
|
||||
function attemptDial (pi, cb) {
|
||||
const tKeys = Object.keys(self.transports)
|
||||
const tKeys = availableTransports(pi)
|
||||
nextTransport(tKeys.shift())
|
||||
|
||||
function nextTransport (key) {
|
||||
@@ -366,6 +374,14 @@ function Swarm (peerInfo) {
|
||||
}
|
||||
}
|
||||
|
||||
// Start listening on all available transports
|
||||
this.listen = (callback) => {
|
||||
parallel(availableTransports(peerInfo).map((ts) => (cb) => {
|
||||
// Listen on the given transport
|
||||
this.transport.listen(ts, {}, null, cb)
|
||||
}), callback)
|
||||
}
|
||||
|
||||
this.handle = (protocol, handler) => {
|
||||
this.protocols[protocol] = handler
|
||||
}
|
||||
|
@@ -66,8 +66,8 @@ describe('high level API - with everything mixed all together!', function () {
|
||||
|
||||
parallel([
|
||||
(cb) => swarmA.transport.listen('tcp', {}, null, cb),
|
||||
(cb) => swarmB.transport.listen('tcp', {}, null, cb),
|
||||
(cb) => swarmC.transport.listen('tcp', {}, null, cb)
|
||||
(cb) => swarmB.transport.listen('tcp', {}, null, cb)
|
||||
// (cb) => swarmC.transport.listen('tcp', {}, null, cb)
|
||||
], done)
|
||||
})
|
||||
|
||||
@@ -86,12 +86,16 @@ describe('high level API - with everything mixed all together!', function () {
|
||||
|
||||
parallel([
|
||||
(cb) => swarmB.transport.listen('ws', {}, null, cb),
|
||||
(cb) => swarmC.transport.listen('ws', {}, null, cb),
|
||||
// (cb) => swarmC.transport.listen('ws', {}, null, cb),
|
||||
(cb) => swarmD.transport.listen('ws', {}, null, cb),
|
||||
(cb) => swarmE.transport.listen('ws', {}, null, cb)
|
||||
], done)
|
||||
})
|
||||
|
||||
it('listen automatically', (done) => {
|
||||
swarmC.listen(done)
|
||||
})
|
||||
|
||||
it('add spdy', () => {
|
||||
swarmA.connection.addStreamMuxer(spdy)
|
||||
swarmB.connection.addStreamMuxer(spdy)
|
||||
|
Reference in New Issue
Block a user