react-native-zeroconf
react-native-zeroconf copied to clipboard
repeated alerts on resolved data
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.
Hum that's weird, are you sure you are not creating multiple zeroconf instances somehow?
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, I solved this by calling
componentDidMount() {
this.zeroconf.removeDeviceListeners();
this.zeroconf.addDeviceListeners();
}
and then again calling this.zeroconf.removeDeviceListeners();
on resolve function.
This conditional should take care of making sure there is only one listener for each event, @nomoreboredom you were getting the issue too?
@Apercu Yes, however I was building the Zeroconf Object and using the resolve function on the constructor, if that makes a difference...
@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..