diff --git a/examples/example.js b/examples/example.js deleted file mode 100644 index c550ce8..0000000 --- a/examples/example.js +++ /dev/null @@ -1,47 +0,0 @@ -'use strict' - -/* - * Spawn a js-ipfs or a go-ipfs daemon and run this example to - * watch it being detected - */ - -const PeerInfo = require('peer-info') -const MulticastDNS = require('./../src') -const multiaddr = require('multiaddr') -const Node = require('libp2p-ipfs-nodejs') -const series = require('async/series') - -const ma = multiaddr('/ip4/127.0.0.1/tcp/0') -let pi -let node - -series([ - (cb) => { - PeerInfo.create((err, peerInfo) => { - if (err) { cb(err) } - pi = peerInfo - pi.multiaddr.add(ma) - cb() - }) - }, - (cb) => { - node = new Node(pi) - node.start(cb) - } -], (err) => { - if (err) { - throw err - } - const options = { - verify: false, - port: 5353 - } - const mdns = new MulticastDNS(node, options) - - console.log('PeerId:', pi.id.toB58String()) - console.log('Looking for other nodes') - - mdns.once('peer', (peerInfo) => { - console.log(peerInfo.id.toB58String()) - }) -}) diff --git a/src/index.js b/src/index.js index 910875c..7ace1d0 100644 --- a/src/index.js +++ b/src/index.js @@ -39,11 +39,16 @@ class MulticastDNS extends EventEmitter { query.gotQuery(event, this.mdns, this.peerInfo, this.serviceTag, this.broadcast) }) - callback() + setImmediate(() => callback()) } stop (callback) { - this.mdns.destroy(callback) + if (!this.mdns) { + callback(new Error('MulticastDNS service had not started yet')) + } else { + this.mdns.destroy(callback) + this.mdns = undefined + } } } diff --git a/src/query.js b/src/query.js index b7387be..178cc8a 100644 --- a/src/query.js +++ b/src/query.js @@ -82,6 +82,10 @@ module.exports = { gotQuery: function (qry, mdns, peerInfo, serviceTag, broadcast) { if (!broadcast) { return } + const multiaddrs = tcp.filter(peerInfo.multiaddrs) + // Only announce TCP for now + if (multiaddrs.length === 0) { return } + if (qry.questions[0] && qry.questions[0].name === serviceTag) { const answers = [] @@ -94,7 +98,6 @@ module.exports = { }) // Only announce TCP multiaddrs for now - const multiaddrs = tcp.filter(peerInfo.multiaddrs) const port = multiaddrs[0].toString().split('/')[4] answers.push({ diff --git a/test/multicast-dns.spec.js b/test/multicast-dns.spec.js index e0c2514..31ee9a4 100644 --- a/test/multicast-dns.spec.js +++ b/test/multicast-dns.spec.js @@ -83,7 +83,9 @@ describe('MulticastDNS', () => { parallel([ (cb) => mdnsA.start(cb), - (cb) => mdnsC.start(cb) + (cb) => mdnsC.start(cb), + (cb) => mdnsD.start(cb) + ], () => { mdnsA.once('peer', (peerInfo) => { expect(pC.id.toB58String()).to.eql(peerInfo.id.toB58String())