becvert / cordova-plugin-zeroconf

Cordova ZeroConf Plugin
MIT License
81 stars 57 forks source link

Unable to see my bonjour service #75

Closed tonetechnician closed 5 years ago

tonetechnician commented 5 years ago

Cordova : v8.1.2 OS : Windows 10 Android : 7.1.1

Hey there, I'm hoping someone can help me here.

I've been trying to use the plugin to find my bonjour service created with npm bonjour.

The service is definitely working as I'm able to find it with a nodejs script. When I use the zeroconf.watch() this is not working. Below is my .js source code in www/.

// In www/src/js
onDeviceReady: function() {
        this.receivedEvent('deviceready');
        var zeroconf = cordova.plugins.zeroconf;

        zeroconf.registerAddressFamily = 'ipv4'; // or 'ipv6' ('any' by default)
        zeroconf.watchAddressFamily = 'ipv4'; // or 'ipv6' ('any' by default)

        zeroconf.watch('_http._tcp', 'local.', function(result) {
            var action = result.action;
            var service = result.service;
            if (action == 'added') {
                console.log('service added', service);
            } else if (action == 'resolved') {
                console.log('service resolved', service);
            } else {
                console.log('service removed', service);
            }
        });
    },

I've initialized the bonjour service using the nodejs script below

// In server 
// index.js
var bonjour = require('bonjour')()

// advertise an HTTP server on port 1000
bonjour.publish({ name: 'my-service', type: 'http', port: 1000 })
console.log("bonjour service published");

Am testing on my real android device

becvert commented 5 years ago

It might be you're missing a dot in _http._tcp => _http._tcp.

tonetechnician commented 5 years ago

Thanks so much for the fast reply. :)

Thanks for noticing that. I've added in the dot and my service is still not showing up.. It seems the callback never happens either.

Not 100% sure why this would be the case.

tonetechnician commented 5 years ago

So just to see if things are working, I've tried to register a service from the phone.

The service is registered successfully and .watch picks it up. My nodejs bonjour also picks it up.

So it seems .watch is unable to pick up my nodejs server's bonjour service advertisement.

Here is my updated code cordova code, any ideas that could shed more light would be great.

    onDeviceReady: function() {
        this.receivedEvent('deviceready');
        var zeroconf = cordova.plugins.zeroconf;
        zeroconf.registerAddressFamily = 'ipv4'; // or 'ipv6' ('any' by default)
        zeroconf.watchAddressFamily = 'ipv4'; // or 'ipv6' ('any' by default)

        zeroconf.watch('_http._tcp.', 'local.', function(result) {
            console.log("in watch")
            var action = result.action;
            var service = result.service;
            if (action == 'added') {
                console.log('service added', service);
            } else if (action == 'resolved') {
                console.log('service resolved', service);
            } else {
                console.log('service removed', service);
            }
        });

        zeroconf.register("_http._tcp.",'local.','phone',80,{'foo' : 'bar'}, (result) => {
            var action = result.action; // 'registered'
            var service = result.service;
            console.log("registered");
        })
    },

For more info, I'm debugging the android app using USB debugging and chrome dev tools. Not sure this would cause any issue though.

tonetechnician commented 5 years ago

Ah, I've solved my problem :)

Turns out my server's bonjour was advertising on the incorrect network that the phone was not connected to. After disabling that network (my virtual box network adapter) everything works as it should.

Thanks for the help @becvert, and thanks for maintaining such a great project!