mdns-js / node-mdns-js

Pure JavaScript/NodeJS mDNS discovery implementation.
Apache License 2.0
161 stars 60 forks source link

Ensure it's possible to create a new Browser after closing a previous one. #56

Closed XavierTalpe closed 7 years ago

XavierTalpe commented 8 years ago

This fixes a bug that made it impossible to create a new browser.js object after closing the original browser.js object.

When closing the object using:

browser.removeAllListeners();
browser.stop();

Any new instance of the browser.js class would not have it's update event triggered. This is due to the original event listener not being cleaned up. The networking.js class also required a fix to properly reset the counter after all users called removeUsage or stopRequest.

Below is a code example that demonstrates the new fixes. Using this with the old code will result in an empty list of services after the first run.

Is it possible to create a new release and publish this one to NPM?

var mdns = require('../');

var services = [];

function create() {
  mdns.excludeInterface('0.0.0.0');
  var browser = mdns.createBrowser(mdns.tcp('workstation')); //defaults to mdns.ServiceType.wildcard

  browser.on('ready', function onReady() {
    browser.discover();
  });

  browser.on('update', function onUpdate(data) {
    services.push(data.fullname);
  });

  return browser;
}

function destroy(browser) {
  browser.removeAllListeners();
  browser.stop();
  services = [];
}

setInterval(function() {
  var browser = create();

  setTimeout(function() {
    console.log(services);

    destroy(browser);
  }, 1000);
}, 2000);
sebakerckhof commented 8 years ago

+1

kmpm commented 8 years ago

I will test it this weekend and publish a new version if everything is fine.

sebakerckhof commented 8 years ago

@kmpm any update on this?