Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some bugfixes #69

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
###### `update`
Triggered either when a service is found, resolved or removed. It returns the service that has been updated.

###### `error` Triggered when an error occurs
Original file line number Diff line number Diff line change
Expand Up @@ -64,39 +64,39 @@ 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
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());
}

@Override
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);
}
};

Expand All @@ -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);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use a simple ternary here to avoid repeating the same code, and send the errorString instead of the params if it's defined.

From my understanding, the errorString couldn't work because it was a String instead of an Object?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, sorry this block is wrong. I´m going to upload a fix for this ;)

}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 {
Expand All @@ -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);
}
}

Expand All @@ -148,18 +160,25 @@ 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);
}
}

service.putMap(KEY_SERVICE_TXT, txtRecords);

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

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "react-native-zeroconf",
"version": "0.9.0",
"description": "A Zeroconf discovery utility for react-native",
"main": "dist",
"main": "src",
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I doubt this is right

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don´t use minifying ;)

"scripts": {
"lint": "eslint src/*.js",
"build": "rm -rf dist && mkdir dist && babel src -o dist/index.js"
Expand Down
30 changes: 20 additions & 10 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export default class Zeroconf extends EventEmitter {
super(props)

this._services = {}
this._resolvedServices = {}
this._dListeners = {}

this.addDeviceListeners()
Expand All @@ -20,7 +21,7 @@ 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.')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why emit a simple string?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only want to know if an error has ocurred (string is enough). If you pass an Error, you will get a red screen in debug mode, and an app crash in release mode

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fair

}

this._dListeners.start = DeviceEventEmitter.addListener('RNZeroconfStart', () => this.emit('start'))
Expand All @@ -32,8 +33,8 @@ export default class Zeroconf extends EventEmitter {
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 => {
Expand All @@ -42,16 +43,17 @@ export default class Zeroconf extends EventEmitter {

delete this._services[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)
})

}
Expand All @@ -65,27 +67,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()
}

}