diff --git a/README.md b/README.md index 4249f74..d380b92 100644 --- a/README.md +++ b/README.md @@ -29,11 +29,15 @@ This will initialize the scan from the `Zeroconf` instance. Will stop another sc ###### `stop()` Stop the scan -If any scan is running, stop it. Otherwise do nothing. +If any scan is running, stop it. Otherwise do nothing. Be sure to do this when the apps go to background, avoids potential memory leaks. ###### `getServices()` Returns resolved services -Will return all names of services that have been resolved. +Will return all names of services that have been found or resolved. + +###### `getResolvedServices()` Returns resolved services + +Will return all names of services that have been fully resolved. ###### `removeDeviceListeners()` Remove listeners @@ -76,5 +80,7 @@ Broadcast a service object once it is fully resolved Broadcast a service name removed from the network. -###### `update` Triggered either when a service is found or removed -###### `error` Triggered when an error occurs +###### `update` +Triggered either when a service is found, resolved or removed. It returns the service that has been updated. + +###### `errorEvent` Triggered when an error occurs. It returns the error code as object (Android) or int(iOS) diff --git a/android/src/main/java/com/balthazargronon/RCTZeroconf/ZeroconfModule.java b/android/src/main/java/com/balthazargronon/RCTZeroconf/ZeroconfModule.java index 2c470ab..4e2f13c 100644 --- a/android/src/main/java/com/balthazargronon/RCTZeroconf/ZeroconfModule.java +++ b/android/src/main/java/com/balthazargronon/RCTZeroconf/ZeroconfModule.java @@ -64,23 +64,23 @@ public void scan(String type, String protocol, String domain) { @Override public void onStartDiscoveryFailed(String serviceType, int errorCode) { String error = "Starting service discovery failed with code: " + errorCode; - sendEvent(getReactApplicationContext(), EVENT_ERROR, error); + sendEvent(getReactApplicationContext(), EVENT_ERROR, null, error); } @Override public void onStopDiscoveryFailed(String serviceType, int errorCode) { String error = "Stopping service discovery failed with code: " + errorCode; - sendEvent(getReactApplicationContext(), EVENT_ERROR, error); + sendEvent(getReactApplicationContext(), EVENT_ERROR, null, error); } @Override public void onDiscoveryStarted(String serviceType) { - sendEvent(getReactApplicationContext(), EVENT_START, null); + sendEvent(getReactApplicationContext(), EVENT_START, null, null); } @Override public void onDiscoveryStopped(String serviceType) { - sendEvent(getReactApplicationContext(), EVENT_STOP, null); + sendEvent(getReactApplicationContext(), EVENT_STOP, null, null); } @Override @@ -88,7 +88,7 @@ public void onServiceFound(NsdServiceInfo serviceInfo) { WritableMap service = new WritableNativeMap(); service.putString(KEY_SERVICE_NAME, serviceInfo.getServiceName()); - sendEvent(getReactApplicationContext(), EVENT_FOUND, service); + sendEvent(getReactApplicationContext(), EVENT_FOUND, service, null); mNsdManager.resolveService(serviceInfo, new ZeroResolveListener()); } @@ -96,7 +96,7 @@ public void onServiceFound(NsdServiceInfo serviceInfo) { public void onServiceLost(NsdServiceInfo serviceInfo) { WritableMap service = new WritableNativeMap(); service.putString(KEY_SERVICE_NAME, serviceInfo.getServiceName()); - sendEvent(getReactApplicationContext(), EVENT_REMOVE, service); + sendEvent(getReactApplicationContext(), EVENT_REMOVE, service, null); } }; @@ -114,10 +114,22 @@ public void stop() { protected void sendEvent(ReactContext reactContext, String eventName, - @Nullable Object params) { - reactContext + @Nullable Object params, + @Nullable String errorString + ) { + if (errorString == null){ + reactContext .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) .emit(eventName, params); + }else{ + WritableMap payload = new WritableNativeMap(); + // Put data to map + payload.putString("error", errorString); + reactContext + .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class) + .emit(eventName, payload); + } + } private class ZeroResolveListener implements NsdManager.ResolveListener { @@ -127,7 +139,7 @@ public void onResolveFailed(NsdServiceInfo serviceInfo, int errorCode) { mNsdManager.resolveService(serviceInfo, this); } else { String error = "Resolving service failed with code: " + errorCode; - sendEvent(getReactApplicationContext(), EVENT_ERROR, error); + sendEvent(getReactApplicationContext(), EVENT_ERROR, null, error); } } @@ -148,7 +160,7 @@ public void onServiceResolved(NsdServiceInfo serviceInfo) { txtRecords.putString(String.format(Locale.getDefault(), "%s", key), String.format(Locale.getDefault(), "%s", recordValue != null ? new String(recordValue, "UTF_8") : "")); } catch (UnsupportedEncodingException e) { String error = "Failed to encode txtRecord: " + e; - sendEvent(getReactApplicationContext(), EVENT_ERROR, error); + sendEvent(getReactApplicationContext(), EVENT_ERROR, null, error); } } @@ -156,10 +168,17 @@ public void onServiceResolved(NsdServiceInfo serviceInfo) { WritableArray addresses = new WritableNativeArray(); addresses.pushString(serviceInfo.getHost().getHostAddress()); + + if (serviceInfo.getHost().getHostAddress().equals("")) { + String notIpErrorString = "Ip not resolved"; + sendEvent(getReactApplicationContext(), EVENT_ERROR, null, notIpErrorString); + mNsdManager.resolveService(serviceInfo, this); + return; + } service.putArray(KEY_SERVICE_ADDRESSES, addresses); - sendEvent(getReactApplicationContext(), EVENT_RESOLVE, service); + sendEvent(getReactApplicationContext(), EVENT_RESOLVE, service, null); } } diff --git a/package.json b/package.json index 0e489bf..7df39b4 100644 --- a/package.json +++ b/package.json @@ -1,8 +1,8 @@ { "name": "react-native-zeroconf", - "version": "0.9.0", + "version": "1.0.0", "description": "A Zeroconf discovery utility for react-native", - "main": "dist", + "main": "src", "scripts": { "lint": "eslint src/*.js", "build": "rm -rf dist && mkdir dist && babel src -o dist/index.js" diff --git a/src/index.js b/src/index.js index 00963bb..567d57f 100644 --- a/src/index.js +++ b/src/index.js @@ -9,6 +9,7 @@ export default class Zeroconf extends EventEmitter { super(props) this._services = {} + this._resolvedServices = {} this._dListeners = {} this.addDeviceListeners() @@ -20,20 +21,21 @@ export default class Zeroconf extends EventEmitter { addDeviceListeners () { if (Object.keys(this._dListeners).length) { - return this.emit('error', new Error('RNZeroconf listeners already in place.')) + return this.emit('error', 'RNZeroconf listeners already in place.') } this._dListeners.start = DeviceEventEmitter.addListener('RNZeroconfStart', () => this.emit('start')) this._dListeners.stop = DeviceEventEmitter.addListener('RNZeroconfStop', () => this.emit('stop')) - this._dListeners.error = DeviceEventEmitter.addListener('RNZeroconfError', err => this.emit('error', err)) + this._dListeners.error = DeviceEventEmitter.addListener('RNZeroconfError', (err) => this.emit('errorEvent', err) + ) this._dListeners.found = DeviceEventEmitter.addListener('RNZeroconfFound', service => { if (!service || !service.name) { return } const { name } = service this._services[name] = service - this.emit('found', name) - this.emit('update') + this.emit('found', service) + this.emit('update', service) }) this._dListeners.remove = DeviceEventEmitter.addListener('RNZeroconfRemove', service => { @@ -41,17 +43,19 @@ export default class Zeroconf extends EventEmitter { const { name } = service delete this._services[name] + delete this._resolvedServices[name] - this.emit('remove', name) - this.emit('update') + this.emit('remove', service) + this.emit('update', service) }) this._dListeners.resolved = DeviceEventEmitter.addListener('RNZeroconfResolved', service => { if (!service || !service.name) { return } + this._resolvedServices[service.name] = service this._services[service.name] = service this.emit('resolved', service) - this.emit('update') + this.emit('update', service) }) } @@ -65,27 +69,35 @@ export default class Zeroconf extends EventEmitter { } /** - * Get all the services already resolved + * Get all the services fully resolved or not */ getServices () { return this._services } + /** + * Get all the services fully resolved + */ + getResolvedServices () { + return this._resolvedServices + } + /** * Scan for Zeroconf services, * Defaults to _http._tcp. on local domain */ scan (type = 'http', protocol = 'tcp', domain = 'local.') { this._services = {} - this.emit('update') + this._resolvedServices = {} + this.emit('start') RNZeroconf.scan(type, protocol, domain) } /** * Stop current scan if any */ - stop () { - RNZeroconf.stop() + async stop () { + await RNZeroconf.stop() } }