Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

var bonjour = require('./index')()

// advertise an HTTP server on port 3000
var service = { name: 'My Web Server', type: 'http', port: 3000 }
bonjour.publish(service)

// browse for all http services
var httpservice = bonjour.find({ type: 'http' }, function (service) {
console.log('Found an HTTP server:', service)
})

setTimeout(function () {
bonjour.unpublishAll(function bye () {
console.log('unpublished service:', service)
})
// wait for down of http service
httpservice.on('down', function () {
// try lookup again with a timeout
bonjour.findOne({ type: service.type, timeout: 1500 }, function (service) {
service && console.log('Found a service!', service)
!service && console.log('There is no such service!')
// then quit properly
console.log('byebye')
bonjour.destroy()
})
})
}, 1500)
6 changes: 6 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,13 @@ Bonjour.prototype.find = function (opts, onup) {

Bonjour.prototype.findOne = function (opts, cb) {
var browser = new Browser(this._server.mdns, opts)
var tout = opts.timeout && setTimeout(function () {
browser.emit('notfound')
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure I like this event. What's your reason for emitting it?

browser.stop()
if (cb) cb(false)
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would make more sense to just call the callback without any arguments instead of false in case the timeout happens

}, opts.timeout)
browser.once('up', function (service) {
clearTimeout(tout)
browser.stop()
if (cb) cb(service)
})
Expand Down