Closed Viniciusvalenca closed 6 years ago
Hello! Yes, this needs to be inside the platform.ready()
call.
Here is an example of code that works: https://github.com/emcniece/ZeroconfDemo/blob/master/www/js/index.js. Please note that this is a Cordova app, not an Ionic Framework app.
Are you using Ionic Framework? I think pure Cordova uses an onDeviceReady
function, whereas Ionic uses platform.ready()
. Can you share more code with us?
Yes i am using ionic too. here is my code
import { Component } from '@angular/core'; import { Platform } from 'ionic-angular'; import { StatusBar } from '@ionic-native/status-bar'; import { SplashScreen } from '@ionic-native/splash-screen';
import { HomePage } from '../pages/home/home'; import { Zeroconf } from '@ionic-native/zeroconf';
@Component({ templateUrl: 'app.html' }) export class MyApp { rootPage:any = HomePage;
constructor(platform: Platform, statusBar: StatusBar, public zeroconf: Zeroconf,splashScreen: SplashScreen) { platform.ready().then(() => { // Okay, so the platform is ready and our plugins are available. // Here you can do any higher level native things you might need. statusBar.styleDefault(); splashScreen.hide(); // watch for services of a specified type console.log('TEST'); this.zeroconf.watch('_http._tcp.', 'local.').subscribe(result => { console.log('result', result); if (result.action == 'added') { console.log('service added', result.service); } else { console.log('service removed', result.service); } });
});
}
}
Thanks @Viniciusvalenca. If you want to format your code nicely, try surrounding it with triple backticks, or indent it by 4 spaces. Tips over here.
import { Component } from '@angular/core';
import { Platform } from 'ionic-angular';
import { StatusBar } from '@ionic-native/status-bar';
import { SplashScreen } from '@ionic-native/splash-screen';
import { HomePage } from '../pages/home/home';
import { Zeroconf } from '@ionic-native/zeroconf';
@Component({
templateUrl: 'app.html'
})
export class MyApp {
rootPage:any = HomePage;
constructor(platform: Platform, statusBar: StatusBar,
public zeroconf: Zeroconf,splashScreen: SplashScreen) {
platform.ready().then(() => {
// Okay, so the platform is ready and our plugins are available.
// Here you can do any higher level native things you might need.
statusBar.styleDefault();
splashScreen.hide();
// watch for services of a specified type
console.log('TEST');
this.zeroconf.watch('_http._tcp.', 'local.').subscribe(result => {
console.log('result', result);
if (result.action == 'added') {
console.log('service added', result.service);
} else {
console.log('service removed', result.service);
}
});
});
}
}
You should see something with this code, so let's troubleshoot your surroundings.
_http._tcp.
services that can be discovered?thanks!!!!! i used another app and found that my services is _arduino._tcp !! now it works like a charm! very thanksssssssssss!!!!
Hello, i have a problem with this zeroconf plugin. My code is bellow, the firts console log with the 'teste' string is printed, but all other is not. and no erros pops up.
The only erro i got is when i put this metodo bellow outside the plataform.ready(), but i think they need to stay inside this.
Anyone can help me? tested with the 7.0.0 and 6.4.0 cordova, version of zeroconf is 1.3.3
Thanks!
/ console.log('TEST'); this.zeroconf.watch('_http._tcp.', 'local.').subscribe(result => { console.log('result', result); if (result.action == 'added') { console.log('service added', result.service); } else { console.log('service removed', result.service); } }); /