Capacitor ZeroConf plugin
npm install capacitor-zeroconf-ltCapacitor ZeroConf plugin (based on capacitor-zeroconf)
This plugin allows you to browse and publish ZeroConf/Bonjour/mDNS services from applications developed using Ionic's Capacitor.
This is not a background service. When the cordova view is destroyed/terminated, publish and watch operations are stopped.
Android, iOS and Electron platforms are supported.
The has been ported from Cordova ZeroConf Plugin.
``bash`
npm install capacitor-zeroconf-lt
npx cap sync
or
`bash`
yarn add capacitor-zeroconf-lt
yarn cap sync
* getHostname()
* register(...)
* unregister(...)
* stop()
* watch(...)
* unwatch(...)
* close()
* Interfaces
`typescript`
getHostname() => Promise<{ hostname: string; }>
Returns: Promise<{ hostname: string; }>
--------------------
`typescript`
register(request: ZeroConfRegisterRequest) => Promise
| Param | Type |
| ------------- | --------------------------------------------------------------------------- |
| request | ZeroConfRegisterRequest |
--------------------
`typescript`
unregister(request: ZeroConfUnregisterRequest) => Promise
| Param | Type |
| ------------- | ------------------------------------------------------------------------------- |
| request | ZeroConfUnregisterRequest |
--------------------
`typescript`
stop() => Promise
--------------------
`typescript`
watch(request: ZeroConfWatchRequest, callback?: ZeroConfWatchCallback | undefined) => Promise
| Param | Type |
| -------------- | --------------------------------------------------------------------- |
| request | ZeroConfWatchRequest |
| callback | ZeroConfWatchCallback |
Returns: Promise<string>
--------------------
`typescript`
unwatch(request: ZeroConfUnwatchRequest) => Promise
| Param | Type |
| ------------- | --------------------------------------------------------------------- |
| request | ZeroConfWatchRequest |
--------------------
`typescript`
close() => Promise
--------------------
#### ZeroConfRegisterRequest
| Prop | Type |
| ----------- | --------------------------------------- |
| port | number |
| props | { [key: string]: string; } |
#### ZeroConfUnregisterRequest
| Prop | Type |
| ---------- | ------------------- |
| name | string |
#### ZeroConfWatchRequest
| Prop | Type |
| ------------ | ------------------- |
| type | string |
| domain | string |
Below you can find an example of what a React component could look like:
`jsx
const MdnsService: React.FC
const options = { type: '_castor-display._tcp.', domain: 'local.' };
useEffect(() => {
let listener: any;
const onDiscover = (result: ZeroConfWatchResult) => {
console.log('mDNS listener result:', result);
};
(ZeroConf as any)
.addListener('discover', onDiscover)
.then((res: any) => (listener = res));
ZeroConf.watch(options)
.then((res: any) => console.log('mDNS success:', res))
.catch((err: any) => console.log('mDNS error:', err));
return () => {
if (listener) {
console.log('removing listener', listener);
if (listener.remove) {
console.log('... using remove()');
listener.remove();
} else {
(ZeroConf as any).removeListener(listener);
}
}
ZeroConf.unwatch(options)
.then(() => {
console.log('unwatch success');
// need to close for Android to rescan
// TODO: try fixing Android implementation
ZeroConf.close().then(() => console.log('close success'));
})
.catch((err: any) => console.log('unwatch error:', err));
};
}, []);
return <>>;
};
``