1.0.6 • Published 2 years ago
capacitor-zeroconf-lt v1.0.6
capacitor-zeroconf-lt
Capacitor 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.
Install
npm install capacitor-zeroconf-lt
npx cap sync
or
yarn add capacitor-zeroconf-lt
yarn cap sync
API
getHostname()
getHostname() => Promise<{ hostname: string; }>
Returns: Promise<{ hostname: string; }>
register(...)
register(request: ZeroConfRegisterRequest) => Promise<void>
Param | Type |
---|---|
request | ZeroConfRegisterRequest |
unregister(...)
unregister(request: ZeroConfUnregisterRequest) => Promise<void>
Param | Type |
---|---|
request | ZeroConfUnregisterRequest |
stop()
stop() => Promise<void>
watch(...)
watch(request: ZeroConfWatchRequest, callback?: ZeroConfWatchCallback | undefined) => Promise<CallbackID>
Param | Type |
---|---|
request | ZeroConfWatchRequest |
callback | ZeroConfWatchCallback |
Returns: Promise<string>
unwatch(...)
unwatch(request: ZeroConfUnwatchRequest) => Promise<void>
Param | Type |
---|---|
request | ZeroConfWatchRequest |
close()
close() => Promise<void>
Interfaces
ZeroConfRegisterRequest
Prop | Type |
---|---|
port | number |
props | { key: string: string; } |
ZeroConfUnregisterRequest
Prop | Type |
---|---|
name | string |
ZeroConfWatchRequest
Prop | Type |
---|---|
type | string |
domain | string |
Example
Below you can find an example of what a React component could look like:
const MdnsService: React.FC<Props> = (props) => {
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 <></>;
};