becvert / cordova-plugin-zeroconf

Cordova ZeroConf Plugin
MIT License
81 stars 57 forks source link

Watcher call twice on each event. #38

Closed home-grown-engineer closed 7 years ago

home-grown-engineer commented 7 years ago

I working on module that should sync in one local network devices and share data between them.

My problem is next:

After I register new device in network, watcher start looking on network and waiting for some event. After device is UP, watcher found a bunch of devices in network and start send their info to my device twice. First two times I receive just undefined address of devices. After 15 - 20 seconds I finally got all information about devices and again twice.

scn-20170725-171849-6yoab

Okay. Then I up new device in network and watcher triggered on it. And again I got information about device twice. Then I down device and watcher triggered on it again and on the first time I receive undefined and second one I again got this device as up device.

I try to up second device and again got data twice: first time is the new device data, and second is the old device that should be off.

scn-20170725-172524-j78f6

Is it problem of zeroconf plugin? Because my code doesn't have anything that can call watcher function twice.

`watchOld() {
        console.log("Watch!")
         this.zeroconf.watch(this.config.connection_type, this.config.domain, (result) => {
               const action = result.action;
               const service = result.service;
                if (action === 'added') {
                   const address = service.ipv4Addresses.length > 1
                    ? service.hostname.replace(/\.$/, '')
                    : service.ipv4Addresses[0];
                console.log("Zeroconf watch result : ", address)
                this.emit('serviceUp', [address], service.port);
            } else if (action === 'removed') {
                this.emit('serviceDown', id);
            }
        }, (err) => {
            throw err;
        });
}`
becvert commented 7 years ago

That's odd. I'll have a look.

if you're interested in ipv4 addresses only, you may be interested in these brand new options for Android:

var zeroconf = cordova.plugins.zeroconf;
zeroconf.registerAddressFamily = 'ipv4'; // or 'ipv6' ('any' by default)
zeroconf.watchAddressFamily = 'ipv4'; // or 'ipv6' ('any' by default)
becvert commented 7 years ago

Could you console.log the whole service object to see what's inside? thank you

home-grown-engineer commented 7 years ago

Okay.

Console log of var zeroconf = cordova.plugins.zeroconf; whole-service

This is screenshot with services of registered object and watcher object.

scn-20170726-105233-h0e8c

So I added zeroconf.registerAddressFamily = 'ipv4';

It helps. I do not received any information with undefined address after watch() was called first time.

But I still have duplicating messages from watcher. scn-20170726-105020-je9jk

I tried to add new device again scn-20170726-105407-ubma2

And then remove it and got added device with undefined address scn-20170726-105512-kdidj

And again add another device scn-20170726-105742-mcurh

I will be glad if you find some solution, because this functionality is very important for me. Yours plugin is unique (any of other plugin can't work with iOS) and I glad that you still working on it.

Thank you!

becvert commented 7 years ago

Please try this branch cordova plugin add https://github.com/becvert/cordova-plugin-zeroconf#1.2.6-dev You should now check if (action === 'resolved') Tell us how it goes.

home-grown-engineer commented 7 years ago

Thank you for answer.

That is great that we have now one more action. Yes, it help remove duplicate items, but sometimes.

I tried test it as much as possible and found next behaviors.

Sometimes on first start watching I have duplicates

duplicate-items

After some time, I again got it. I do not off application between time on first screenshot and second.

duplicate-items-again

Next flow will be with deleting.

I restart my application without any devices in network and then UP couples devices and again saw duplicates.

duplicate-items-again1

I tried to remove on of them

remove-items

And start watching some interesting flow of watcher

flow-items

So resolved action helps. But do not fix duplicating sometimes and not fix caching.

If you need, I can record video with flow of adding and removing devices in network.

Thanks for your help!

becvert commented 7 years ago

OK. I'm not sure I can do more than that. I'm just passing on the results of the operations of the underlying libraries.

Duplicates could be explained by the following;

I'd say at this stage you should handle duplicates at the application level. Figure out what you can do according to the added, resolved and removed actions.

Look into the code for improvements. Learn how the the mdns/bonjour protocols work.

I can't spend much more time on this right now. But thanks to you, I feel this plugin's been improved. I'll do a new release sometime soon

home-grown-engineer commented 7 years ago

@becvert Okay, I understand. Thank you for you help 👍