index.js 922 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. 'use strict'
  2. var Registry = require('./lib/registry')
  3. var Server = require('./lib/mdns-server')
  4. var Browser = require('./lib/browser')
  5. module.exports = Bonjour
  6. function Bonjour (opts) {
  7. if (!(this instanceof Bonjour)) return new Bonjour(opts)
  8. this._server = new Server(opts)
  9. this._registry = new Registry(this._server)
  10. }
  11. Bonjour.prototype.publish = function (opts) {
  12. return this._registry.publish(opts)
  13. }
  14. Bonjour.prototype.unpublishAll = function (cb) {
  15. this._registry.unpublishAll(cb)
  16. }
  17. Bonjour.prototype.find = function (opts, onup) {
  18. return new Browser(this._server.mdns, opts, onup)
  19. }
  20. Bonjour.prototype.findOne = function (opts, cb) {
  21. var browser = new Browser(this._server.mdns, opts)
  22. browser.once('up', function (service) {
  23. browser.stop()
  24. if (cb) cb(service)
  25. })
  26. return browser
  27. }
  28. Bonjour.prototype.destroy = function () {
  29. this._registry.destroy()
  30. this._server.mdns.destroy()
  31. }