Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
20 changes: 11 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ cordova plugin add cordova-zeroconf-plugin
or, for older versions:

```bash
cordova plugin add https://github.com/vstirbu/ZeroConf@1.1.1
cordova plugin add https://github.com/vstirbu/ZeroConf@1.3.0
```

## Using the plugin ##

There are six static methods on the ZeroConf object, as follows:

### `watch(type, callback)`
Note that `type` is a fully-qualified service type, including the domain, e.g. `"_http._tcp.local."`
### `watch(service, domain, callback)`
Note that `service` is a fully-qualified service type, e.g. `"_http._tcp."`

As `domain` consider using `local.` (dot at the end!)

`callback` is a function that is called when services are added and removed. The function is passed
an object with the following structure:
Expand All @@ -32,13 +34,13 @@ an object with the following structure:
{
"service": {
"port": 50930,
"protocol": "tcp",
"application": "http",
"urls": ["http://192.168.2.2:50930", "http://fe80::7256:81ff:fe00:99e3:50930"],
"description": "\\00",
"name": "Black iPod",
"protocol": "tcp", #android only
"application": "http", #android only
"urls": ["http://192.168.2.2:50930", "http://fe80::7256:81ff:fe00:99e3:50930"], ##android only
"description": "\\00", ##android only
"name": "Black iPod",
"domain": "local",
"server": "",
"server": "", ##android only
"addresses": ["192.168.2.2", "fe80::7256:81ff:fe00:99e3"],
"type": "_http._tcp.local.",
"qualifiedname": "Black iPod._http._tcp.local."
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"cordova": {
"id": "cordova-zeroconf-plugin",
"platforms": [
"ios",
"android"
]
},
Expand All @@ -22,7 +23,7 @@
"version": ">=3.0.0"
}
],
"author": "Vlad Stirbu",
"author": "Vlad Stirbu, Jonas Schnelli",
"license": "MIT",
"bugs": {
"url": "https://github.com/vstirbu/ZeroConf/issues"
Expand Down
17 changes: 15 additions & 2 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,34 @@
<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="cordova-zeroconf-plugin"
version="1.2.0">
version="1.3.0">
<engines>
<engine name="cordova" version=">=3.0.0" />
</engines>

<name>ZeroConf</name>
<description>ZeroConf plugin for Cordova/Phonegap</description>
<author>Vlad Stirbu</author>
<author>Vlad Stirbu, Jonas Schnelli</author>
<keywords>zeroconf</keywords>
<license>MIT</license>

<js-module src="www/ZeroConf.js" name="zeroconf">
<clobbers target="ZeroConf" />
</js-module>

<!-- ios -->
<platform name="ios">
<!-- Cordova >= 2.8 -->
<config-file target="config.xml" parent="/*">
<feature name="ZeroConf">
<param name="ios-package" value="CDVZeroConf" />
<param name="onload" value="true" />
</feature>
</config-file>

<source-file src="src/ios/CDVZeroConf.mm"/>
</platform>

<platform name="android">

<config-file target="AndroidManifest.xml" parent="/manifest">
Expand Down
7 changes: 4 additions & 3 deletions src/android/ZeroConf.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ public boolean execute(String action, JSONArray args,
this.callback = callbackContext;

if (action.equals("watch")) {
final String type = args.optString(0);
if (type != null) {
final String service = args.optString(0);
final String domain = args.optString(1);
if (service != null) {
cordova.getThreadPool().execute(new Runnable() {
public void run() {
watch(type); // Thread-safe.
watch(service+domain); // Thread-safe.
}
});
} else {
Expand Down
95 changes: 95 additions & 0 deletions src/ios/CDVZeroConf.mm
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/**
* ZeroConf plugin for Cordova/Phonegap
*
* Copyright (c) 2016 Jonas Schnelli <dev@jonasschnelli.ch>
* MIT License
*
* @author Jonas Schnelli
* Available under the terms of the MIT License.
*
* TODO currently, only the watch command is supported.
* TODO the iOS implementation should support the same
* TODO features as the android version.
*
*/

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <Foundation/NSNetServices.h>

#import <Cordova/CDVPlugin.h>
#import <arpa/inet.h>

@interface CDVZeroConf : CDVPlugin <NSNetServiceBrowserDelegate, NSNetServiceDelegate> {
NSNetServiceBrowser* _netServiceBrowser;
}
@property NSNetServiceBrowser* netServiceBrowser;
@property NSString* watchCommandCallbackId;
@property (nonatomic, strong) NSMutableArray *services;
- (void)watch:(CDVInvokedUrlCommand*)command;
@end

@implementation CDVZeroConf

+ (NSString *) stringFromAddress: (const struct sockaddr *) address
{
if (address && address->sa_family == AF_INET)
{
const struct sockaddr_in* sin = (struct sockaddr_in *) address;
return [NSString stringWithFormat:@"%@", [NSString stringWithUTF8String:inet_ntoa(sin->sin_addr)]];
}

return nil;
}

- (void)watch:(CDVInvokedUrlCommand*)command
{
self.services = [[NSMutableArray alloc] init];
[self.netServiceBrowser stop];
self.netServiceBrowser = [[NSNetServiceBrowser alloc] init];
self.netServiceBrowser.delegate = self;
[self.netServiceBrowser searchForServicesOfType:command.arguments[0] inDomain:command.arguments[1]];

//store the callback id
self.watchCommandCallbackId = command.callbackId;
}

- (void)netServiceBrowser:(NSNetServiceBrowser*)netServiceBrowser didFindService:(NSNetService*)service moreComing:(BOOL)moreComing {

if (!self.services) {
self.services = [[NSMutableArray alloc] init];
}
[self.services addObject:service];
[service setDelegate:self];
[service resolveWithTimeout:5.0];
}

// Sent when addresses are resolved
- (void)netServiceDidResolveAddress:(NSNetService *)service
{
NSMutableArray *addrs = [NSMutableArray array];
NSLog(@"%@, %@, %@, %ld", service.name, service.domain, service.addresses, service.port);
for (NSData *data in service.addresses)
{
struct sockaddr *adr = (struct sockaddr*)data.bytes;
NSString *ipStr = [CDVZeroConf stringFromAddress:adr];
if (ipStr)
[addrs addObject:ipStr];
}
NSDictionary *serviceJson = @{
@"domain" : service.domain,
@"name" : service.name,
@"type" : service.type,
@"port" : [NSNumber numberWithInteger:service.port],
@"addresses" : addrs};
NSDictionary *jsonObj = @{@"action" : @"Added", @"service" : serviceJson};

CDVPluginResult *pluginResult = [ CDVPluginResult
resultWithStatus : CDVCommandStatus_OK
messageAsDictionary : jsonObj
];

[self.commandDelegate sendPluginResult:pluginResult callbackId:self.watchCommandCallbackId];
}

@end
4 changes: 2 additions & 2 deletions www/ZeroConf.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
var exec = require('cordova/exec');

var ZeroConf = {
watch: function (type, callback) {
watch: function (service, domain, callback) {
return exec(function (result) {
if (callback) {
callback(result);
}

}, ZeroConf.fail, "ZeroConf", "watch", [type]);
}, ZeroConf.fail, "ZeroConf", "watch", [service, domain]);
},
unwatch: function (type) {
return exec(null, ZeroConf.fail, "ZeroConf", "unwatch", [type]);
Expand Down