Zeroconf
This plugin allows you to browse and publish Zeroconf/Bonjour/mDNS services.
Repo: https://github.com/becvert/cordova-plugin-zeroconf
Installation
- Install the Cordova and Ionic Native plugins:
$ ionic cordova plugin add cordova-plugin-zeroconf $ npm install --save @ionic-native/zeroconf@4 - Add this plugin to your app's module
Supported platforms
- Android
- iOS
Usage
import { Zeroconf } from '@ionic-native/zeroconf';
constructor(private zeroconf: Zeroconf) { }
...
// watch for services of a specified type
this.zeroconf.watch('_http._tcp.', 'local.').subscribe(result => {
if (result.action="/?originalUrl=https%3A%2F%2Fionicframework.com%2F%3D%2520%26%2339%3Badded%26%2339%3B)%2520%7B%2520%2520%2520%2520console.log(%26%2339%3Bservice%2520added%26%2339%3B%2C%2520result.service)%3B%2520%2520%7D%2520else%2520%7B%2520%2520%2520%2520console.log(%26%2339%3Bservice%2520removed%26%2339%3B%2C%2520result.service)%3B%2520%2520%7D%7D)%3B%2F%2F%2520publish%2520a%2520zeroconf%2520service%2520of%2520your%2520ownthis.zeroconf.register(%26%2339%3B_http._tcp.%26%2339%3B%2C%2520%26%2339%3Blocal.%26%2339%3B%2C%2520%26%2339%3BBecvert%5C%26%2339%3Bs%2520iPad%26%2339%3B%2C%252080%2C%2520%7B%2520%2520%26%2339%3Bfoo%26%2339%3B%3A%2520%26%2339%3Bbar%26%2339%3B%7D).then(result%2520%3D%26gt%3B%2520%7B%2520%2520console.log(%26%2339%3BService%2520registered%26%2339%3B%2C%2520result.service)%3B%7D)%3B%2F%2F%2520unregister%2520your%2520servicethis.zeroconf.unregister(%26%2339%3B_http._tcp.%26%2339%3B%2C%2520%26%2339%3Blocal.%26%2339%3B%2C%2520%26%2339%3BBecvert%5C%26%2339%3Bs%2520iPad%26%2339%3B)%3B%253C%2Fcode">
Instance Members
getHostname()
Returns this device’s hostname.
Returns: Promise<string>
register(type, domain, name, port, txtRecord)
Publishes a new service.
| Param | Type | Details |
|---|---|---|
| type |
string
|
Service type name, e.g. "_http._tcp". |
| domain |
string
|
Domain scope of the service, typically "local.". |
| name |
string
|
Unqualified service instance name. |
| port |
number
|
Local port on which the service runs. |
| txtRecord |
any
|
Arbitrary key/value pairs describing the service. |
Returns: Promise<ZeroconfResult> Returns a Promise that resolves with the registered service.
unregister(type, domain, name)
Unregisters a service.
| Param | Type | Details |
|---|---|---|
| type |
string
|
Service type name, e.g. "_http._tcp". |
| domain |
string
|
Domain scope of the service, typically "local.". |
| name |
string
|
Unqualified service instance name. |
Returns: Promise<void>
stop()
Unregisters all published services.
Returns: Promise<void>
watch(type, domain)
Starts watching for services of the specified type.
| Param | Type | Details |
|---|---|---|
| type |
string
|
Service type name, e.g. "_http._tcp". |
| domain |
string
|
Domain scope of the service, typically "local.". |
Returns: Observable<ZeroconfResult> Returns an Observable that notifies of each service added or removed.
unwatch(type, domain)
Stops watching for services of the specified type.
| Param | Type | Details |
|---|---|---|
| type |
string
|
Service type name, e.g. "_http._tcp". |
| domain |
string
|
Domain scope of the service, typically "local.". |
Returns: Promise<void>
close()
Closes the service browser and stops watching.
Returns: Promise<void>
reInit()
Re-initializes the plugin to clean service & browser state.
Returns: Promise<void>
registerAddressFamily
Family of addresses to register: ipv4, ipv6 or any.
watchAddressFamily
Family of addresses to watch for: ipv4, ipv6 or any.