@inthepocket/react-native-service-discovery
Version:
mDNS Network Service Discovery in React Native
73 lines (66 loc) • 2.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.stopSearch = exports.startSearch = exports.addEventListener = void 0;
var _reactNative = require("react-native");
const LINKING_ERROR = `The package '@inthepocket/react-native-service-discovery' doesn't seem to be linked. Make sure: \n\n` + _reactNative.Platform.select({
ios: "- You have run 'pod install'\n",
default: ''
}) + '- You rebuilt the app after installing the package\n' + '- You are not using Expo Go\n';
// @ts-expect-error
const isTurboModuleEnabled = global.__turboModuleProxy != null;
const ServiceDiscoveryModule = isTurboModuleEnabled ? require('./NativeServiceDiscovery').default : _reactNative.NativeModules.ServiceDiscovery;
const ServiceDiscovery = ServiceDiscoveryModule ? ServiceDiscoveryModule : new Proxy({}, {
get() {
throw new Error(LINKING_ERROR);
}
});
/**
* A service that is found or lost on the network.
*/
const EventEmitter = new _reactNative.NativeEventEmitter(ServiceDiscovery);
/**
*
* @example
* ```ts
* import * as ServiceDiscovery from '@inthepocket/react-native-service-discovery';
*
* ServiceDiscovery.addEventListener('serviceFound', (service) => console.log('Service found:', service));
*
* // Start searching for _http._tcp. services
* await ServiceDiscovery.startSearch('http');
* ```
*/
const addEventListener = (event, listener) => EventEmitter.addListener(event, listener);
/**
* Start searching for services of a specific type. Searching for
* multiple services can be done in parallel.
*
* @param serviceType The service type to search for, e.g. 'http'. The device
* will search for services of type `_serviceType._tcp.`.
*
* @example
* ```ts
* import * as ServiceDiscovery from '@inthepocket/react-native-service-discovery';
*
* ServiceDiscovery.addEventListener('serviceFound', (service) => console.log('Service found:', service));
*
* // Start searching for _http._tcp. services
* await ServiceDiscovery.startSearch('http');
* ```
*/
exports.addEventListener = addEventListener;
const startSearch = serviceType => ServiceDiscovery.startSearch(serviceType);
/**
* Stop searching for a specific service. This will not stop searching for any
* other services that are being searched for.
*
* @returns A promise that resolves when the search has been stopped, and
* rejects if the search cannot be stopped. Does not reject if the search has
* already been stopped.
*/
exports.startSearch = startSearch;
const stopSearch = serviceType => ServiceDiscovery.stopSearch(serviceType);
exports.stopSearch = stopSearch;
//# sourceMappingURL=index.js.map