react-native-zeroconf icon indicating copy to clipboard operation
react-native-zeroconf copied to clipboard

repeated alerts on resolved data

Open amirmatin opened this issue 7 years ago • 6 comments

first, thanks for the great plugin, works really well .. I am facing a strange issue ( it might be that I am not using the plugin correctly ) and it is when I call zero conf scan and on.resolve, I alert the resolved data however the for the number of times I call the method, number of alerts increment. ie the first time I call method i get one alert, second time i call the method i get 2 alerts at the same time and so on... BTW there is a time gap between my calls (3 secs ..)

below is the method that I call..

  method_zeroConf(type = "osc", protocol = "tcp", domain = "local.") {
    //zeroconf.on('stop', () => alert('stop any running scan'));
    try {
      zeroconf.stop();
      //zeroconf.removeDeviceListeners();
      zeroconf.scan(type, protocol, domain);
      //zeroconf.on('start', () => alert('started'));
      //zeroconf.on('found', () => alert('found.......'));
      zeroconf.on("resolved", data => alert(JSON.stringify(data, null, "\t")));
    }
    catch(err) {
      alert('z-conf was not successful !', err);
    }
  }  

now is there a way to clear the data before scan ? thanks.

amirmatin avatar Oct 11 '17 12:10 amirmatin

Hum that's weird, are you sure you are not creating multiple zeroconf instances somehow?

balthazar avatar Oct 11 '17 15:10 balthazar

Don't think so..., its only created when the page is loading.. before defining my class..

const zeroconf = new Zeroconf();

export default class LayoutExample extends React.Component { constructor(props) { super(props); .. ..

amirmatin avatar Oct 11 '17 16:10 amirmatin

@amirmatin, I solved this by calling

componentDidMount() {
  this.zeroconf.removeDeviceListeners();
  this.zeroconf.addDeviceListeners();
} 

and then again calling this.zeroconf.removeDeviceListeners(); on resolve function.

nomoreboredom avatar Oct 12 '17 08:10 nomoreboredom

This conditional should take care of making sure there is only one listener for each event, @nomoreboredom you were getting the issue too?

balthazar avatar Oct 12 '17 16:10 balthazar

@Apercu Yes, however I was building the Zeroconf Object and using the resolve function on the constructor, if that makes a difference...

nomoreboredom avatar Oct 12 '17 17:10 nomoreboredom

@Apercu Thanks for your response.. I still have the issue, I added the componentDidMount before my render () and removeDeviceListeners() after resolve() but still get duplicates.. can you please share a sample code ?

thanks..

amirmatin avatar Oct 19 '17 11:10 amirmatin