1.0.6 • Published 2 years ago

capacitor-zeroconf-lt v1.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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>
ParamType
requestZeroConfRegisterRequest

unregister(...)

unregister(request: ZeroConfUnregisterRequest) => Promise<void>
ParamType
requestZeroConfUnregisterRequest

stop()

stop() => Promise<void>

watch(...)

watch(request: ZeroConfWatchRequest, callback?: ZeroConfWatchCallback | undefined) => Promise<CallbackID>
ParamType
requestZeroConfWatchRequest
callbackZeroConfWatchCallback

Returns: Promise<string>


unwatch(...)

unwatch(request: ZeroConfUnwatchRequest) => Promise<void>
ParamType
requestZeroConfWatchRequest

close()

close() => Promise<void>

Interfaces

ZeroConfRegisterRequest

PropType
portnumber
props{ key: string: string; }

ZeroConfUnregisterRequest

PropType
namestring

ZeroConfWatchRequest

PropType
typestring
domainstring

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 <></>;
};