@cappitolian/network-discovery 0.0.9 → 0.0.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,116 +1,116 @@
1
- package com.cappitolian.plugins.networkdiscovery;
2
-
3
- import com.getcapacitor.JSObject;
4
- import com.getcapacitor.Plugin;
5
- import com.getcapacitor.PluginCall;
6
- import com.getcapacitor.PluginMethod;
7
- import com.getcapacitor.annotation.CapacitorPlugin;
8
-
9
- @CapacitorPlugin(name = "NetworkDiscovery")
10
- public class NetworkDiscoveryPlugin extends Plugin {
11
- private NetworkDiscovery implementation;
12
-
13
- @Override
14
- public void load() {
15
- implementation = new NetworkDiscovery(this, getContext());
16
- }
17
-
18
- @PluginMethod
19
- public void startAdvertising(PluginCall call) {
20
- String serviceName = call.getString("serviceName");
21
- String serviceType = call.getString("serviceType");
22
- Integer port = call.getInt("port");
23
- JSObject txtRecord = call.getObject("txtRecord");
24
-
25
- if (serviceName == null || serviceType == null || port == null) {
26
- call.reject("Missing required parameters");
27
- return;
28
- }
29
-
30
- implementation.startAdvertising(
31
- serviceName,
32
- serviceType,
33
- port,
34
- txtRecord,
35
- new NetworkDiscovery.AdvertisingCallback() {
36
- @Override
37
- public void onSuccess() {
38
- JSObject ret = new JSObject();
39
- ret.put("success", true);
40
- call.resolve(ret);
41
- }
42
-
43
- @Override
44
- public void onError(String error) {
45
- call.reject(error);
46
- }
47
- }
48
- );
49
- }
50
-
51
- @PluginMethod
52
- public void stopAdvertising(PluginCall call) {
53
- implementation.stopAdvertising(new NetworkDiscovery.StopCallback() {
54
- @Override
55
- public void onSuccess() {
56
- JSObject ret = new JSObject();
57
- ret.put("success", true);
58
- call.resolve(ret);
59
- }
60
-
61
- @Override
62
- public void onError(String error) {
63
- call.reject(error);
64
- }
65
- });
66
- }
67
-
68
- @PluginMethod
69
- public void startDiscovery(PluginCall call) {
70
- String serviceType = call.getString("serviceType");
71
-
72
- if (serviceType == null) {
73
- call.reject("Missing serviceType parameter");
74
- return;
75
- }
76
-
77
- implementation.startDiscovery(serviceType, new NetworkDiscovery.DiscoveryCallback() {
78
- @Override
79
- public void onDiscoveryStarted() {
80
- call.resolve();
81
- }
82
-
83
- @Override
84
- public void onServiceFound(JSObject service) {
85
- notifyListeners("serviceFound", service);
86
- }
87
-
88
- @Override
89
- public void onServiceLost(JSObject service) {
90
- notifyListeners("serviceLost", service);
91
- }
92
-
93
- @Override
94
- public void onError(String error) {
95
- call.reject(error);
96
- }
97
- });
98
- }
99
-
100
- @PluginMethod
101
- public void stopDiscovery(PluginCall call) {
102
- implementation.stopDiscovery(new NetworkDiscovery.StopCallback() {
103
- @Override
104
- public void onSuccess() {
105
- JSObject ret = new JSObject();
106
- ret.put("success", true);
107
- call.resolve(ret);
108
- }
109
-
110
- @Override
111
- public void onError(String error) {
112
- call.reject(error);
113
- }
114
- });
115
- }
1
+ package com.cappitolian.plugins.networkdiscovery;
2
+
3
+ import com.getcapacitor.JSObject;
4
+ import com.getcapacitor.Plugin;
5
+ import com.getcapacitor.PluginCall;
6
+ import com.getcapacitor.PluginMethod;
7
+ import com.getcapacitor.annotation.CapacitorPlugin;
8
+
9
+ @CapacitorPlugin(name = "NetworkDiscovery")
10
+ public class NetworkDiscoveryPlugin extends Plugin {
11
+ private NetworkDiscovery implementation;
12
+
13
+ @Override
14
+ public void load() {
15
+ implementation = new NetworkDiscovery(this, getContext());
16
+ }
17
+
18
+ @PluginMethod
19
+ public void startAdvertising(PluginCall call) {
20
+ String serviceName = call.getString("serviceName");
21
+ String serviceType = call.getString("serviceType");
22
+ Integer port = call.getInt("port");
23
+ JSObject txtRecord = call.getObject("txtRecord");
24
+
25
+ if (serviceName == null || serviceType == null || port == null) {
26
+ call.reject("Missing required parameters");
27
+ return;
28
+ }
29
+
30
+ implementation.startAdvertising(
31
+ serviceName,
32
+ serviceType,
33
+ port,
34
+ txtRecord,
35
+ new NetworkDiscovery.AdvertisingCallback() {
36
+ @Override
37
+ public void onSuccess() {
38
+ JSObject ret = new JSObject();
39
+ ret.put("success", true);
40
+ call.resolve(ret);
41
+ }
42
+
43
+ @Override
44
+ public void onError(String error) {
45
+ call.reject(error);
46
+ }
47
+ }
48
+ );
49
+ }
50
+
51
+ @PluginMethod
52
+ public void stopAdvertising(PluginCall call) {
53
+ implementation.stopAdvertising(new NetworkDiscovery.StopCallback() {
54
+ @Override
55
+ public void onSuccess() {
56
+ JSObject ret = new JSObject();
57
+ ret.put("success", true);
58
+ call.resolve(ret);
59
+ }
60
+
61
+ @Override
62
+ public void onError(String error) {
63
+ call.reject(error);
64
+ }
65
+ });
66
+ }
67
+
68
+ @PluginMethod
69
+ public void startDiscovery(PluginCall call) {
70
+ String serviceType = call.getString("serviceType");
71
+
72
+ if (serviceType == null) {
73
+ call.reject("Missing serviceType parameter");
74
+ return;
75
+ }
76
+
77
+ implementation.startDiscovery(serviceType, new NetworkDiscovery.DiscoveryCallback() {
78
+ @Override
79
+ public void onDiscoveryStarted() {
80
+ call.resolve();
81
+ }
82
+
83
+ @Override
84
+ public void onServiceFound(JSObject service) {
85
+ notifyListeners("serviceFound", service);
86
+ }
87
+
88
+ @Override
89
+ public void onServiceLost(JSObject service) {
90
+ notifyListeners("serviceLost", service);
91
+ }
92
+
93
+ @Override
94
+ public void onError(String error) {
95
+ call.reject(error);
96
+ }
97
+ });
98
+ }
99
+
100
+ @PluginMethod
101
+ public void stopDiscovery(PluginCall call) {
102
+ implementation.stopDiscovery(new NetworkDiscovery.StopCallback() {
103
+ @Override
104
+ public void onSuccess() {
105
+ JSObject ret = new JSObject();
106
+ ret.put("success", true);
107
+ call.resolve(ret);
108
+ }
109
+
110
+ @Override
111
+ public void onError(String error) {
112
+ call.reject(error);
113
+ }
114
+ });
115
+ }
116
116
  }
@@ -13,22 +13,13 @@ import Foundation
13
13
  port: Int,
14
14
  txtRecord: [String: String]?
15
15
  ) {
16
- // Normalizar service type (agregar punto final si no lo tiene)
17
- let normalizedServiceType = serviceType.hasSuffix(".") ? serviceType : serviceType + "."
18
-
19
- print("📡 iOS: Starting advertising")
20
- print(" Service name: \(serviceName)")
21
- print(" Service type (normalized): \(normalizedServiceType)")
22
- print(" Port: \(port)")
23
-
24
- netService = NetService(domain: "local.", type: normalizedServiceType, name: serviceName, port: Int32(port))
16
+ netService = NetService(domain: "local.", type: serviceType, name: serviceName, port: Int32(port))
25
17
 
26
18
  // Configurar TXT record
27
19
  if let txtRecord = txtRecord, !txtRecord.isEmpty {
28
20
  var txtData: [String: Data] = [:]
29
21
  for (key, value) in txtRecord {
30
22
  txtData[key] = value.data(using: .utf8)
31
- print(" TXT Record: \(key) = \(value)")
32
23
  }
33
24
  let txtRecordData = NetService.data(fromTXTRecord: txtData)
34
25
  netService?.setTXTRecord(txtRecordData)
@@ -39,26 +30,17 @@ import Foundation
39
30
  }
40
31
 
41
32
  @objc public func stopAdvertising() {
42
- print("⛔ iOS: Stopping advertising")
43
33
  netService?.stop()
44
34
  netService = nil
45
35
  }
46
36
 
47
37
  @objc public func startDiscovery(serviceType: String, domain: String = "local.") {
48
- // ✅ Normalizar service type
49
- let normalizedServiceType = serviceType.hasSuffix(".") ? serviceType : serviceType + "."
50
-
51
- print("🔍 iOS: Starting discovery")
52
- print(" Service type (normalized): \(normalizedServiceType)")
53
- print(" Domain: \(domain)")
54
-
55
38
  netServiceBrowser = NetServiceBrowser()
56
39
  netServiceBrowser?.delegate = self
57
- netServiceBrowser?.searchForServices(ofType: normalizedServiceType, inDomain: domain)
40
+ netServiceBrowser?.searchForServices(ofType: serviceType, inDomain: domain)
58
41
  }
59
42
 
60
43
  @objc public func stopDiscovery() {
61
- print("⛔ iOS: Stopping discovery")
62
44
  netServiceBrowser?.stop()
63
45
  netServiceBrowser = nil
64
46
  discoveredServices.removeAll()
@@ -67,60 +49,39 @@ import Foundation
67
49
 
68
50
  // MARK: - NetServiceDelegate
69
51
  extension NetworkDiscovery: NetServiceDelegate {
70
- public func netServiceWillPublish(_ sender: NetService) {
71
- print("🔄 iOS: Service WILL publish: \(sender.name)")
72
- }
73
-
74
52
  public func netServiceDidPublish(_ sender: NetService) {
75
- print("✅ iOS: Service published successfully")
76
- print(" Name: \(sender.name)")
77
- print(" Type: \(sender.type)")
78
- print(" Port: \(sender.port)")
53
+ print("Service published: \(sender.name)")
79
54
  delegate?.advertisingDidStart()
80
55
  }
81
56
 
82
57
  public func netService(_ sender: NetService, didNotPublish errorDict: [String : NSNumber]) {
83
- print("❌ iOS: Service did NOT publish")
84
- print(" Error dict: \(errorDict)")
58
+ print("Service did not publish: \(errorDict)")
85
59
  let errorCode = errorDict[NetService.errorCode]?.intValue ?? -1
86
- print(" Error code: \(errorCode)")
87
60
  delegate?.advertisingDidFail(error: "Failed to publish service. Error code: \(errorCode)")
88
61
  }
89
62
  }
90
63
 
91
64
  // MARK: - NetServiceBrowserDelegate
92
65
  extension NetworkDiscovery: NetServiceBrowserDelegate {
93
- public func netServiceBrowserWillSearch(_ browser: NetServiceBrowser) {
94
- print("🔄 iOS: Browser WILL search")
95
- }
96
-
97
66
  public func netServiceBrowserDidStopSearch(_ browser: NetServiceBrowser) {
98
- print(" iOS: Browser DID stop search")
67
+ print("Service discovery stopped")
99
68
  }
100
69
 
101
70
  public func netServiceBrowser(_ browser: NetServiceBrowser, didNotSearch errorDict: [String : NSNumber]) {
102
- print(" iOS: Browser did NOT search")
103
- print(" Error dict: \(errorDict)")
71
+ print("Service discovery failed: \(errorDict)")
104
72
  let errorCode = errorDict[NetService.errorCode]?.intValue ?? -1
105
- print(" Error code: \(errorCode)")
106
73
  delegate?.discoveryDidFail(error: "Discovery failed. Error code: \(errorCode)")
107
74
  }
108
75
 
109
76
  public func netServiceBrowser(_ browser: NetServiceBrowser, didFind service: NetService, moreComing: Bool) {
110
- print(" iOS: Service FOUND!")
111
- print(" Name: \(service.name)")
112
- print(" Type: \(service.type)")
113
- print(" Domain: \(service.domain)")
114
- print(" More coming: \(moreComing)")
115
-
77
+ print("Service found: \(service.name)")
116
78
  discoveredServices.append(service)
117
79
  service.delegate = self
118
80
  service.resolve(withTimeout: 5.0)
119
81
  }
120
82
 
121
83
  public func netServiceBrowser(_ browser: NetServiceBrowser, didRemove service: NetService, moreComing: Bool) {
122
- print(" iOS: Service LOST")
123
- print(" Name: \(service.name)")
84
+ print("Service lost: \(service.name)")
124
85
 
125
86
  let serviceData: [String: Any] = [
126
87
  "serviceName": service.name,
@@ -134,33 +95,46 @@ extension NetworkDiscovery: NetServiceBrowserDelegate {
134
95
  }
135
96
  }
136
97
 
137
- public func netServiceWillResolve(_ sender: NetService) {
138
- print("🔄 iOS: Service WILL resolve: \(sender.name)")
139
- }
140
-
141
98
  public func netServiceDidResolveAddress(_ sender: NetService) {
142
- print(" iOS: Service RESOLVED")
143
- print(" Name: \(sender.name)")
144
- print(" Host: \(sender.hostName ?? "nil")")
145
- print(" Port: \(sender.port)")
99
+ print("Service resolved: \(sender.name)")
100
+
101
+ var addresses: [String] = []
102
+
103
+ if let addressesData = sender.addresses {
104
+ for addressData in addressesData {
105
+ let address = addressData.withUnsafeBytes { (pointer: UnsafeRawBufferPointer) -> String? in
106
+ guard let baseAddress = pointer.baseAddress else { return nil }
107
+ let data = baseAddress.assumingMemoryBound(to: sockaddr.self)
108
+
109
+ var hostname = [CChar](repeating: 0, count: Int(NI_MAXHOST))
110
+ if getnameinfo(data, socklen_t(addressData.count), &hostname, socklen_t(hostname.count), nil, 0, NI_NUMERICHOST) == 0 {
111
+ return String(cString: hostname)
112
+ }
113
+ return nil
114
+ }
115
+
116
+ if let addr = address {
117
+ addresses.append(addr)
118
+ }
119
+ }
120
+ }
146
121
 
147
- // ✅ NO incluir addresses - Solo usar txtRecord
148
122
  var serviceData: [String: Any] = [
149
123
  "serviceName": sender.name,
150
124
  "serviceType": sender.type,
151
125
  "domain": sender.domain,
152
126
  "hostName": sender.hostName ?? "",
153
- "port": sender.port
127
+ "port": sender.port,
128
+ "addresses": addresses
154
129
  ]
155
130
 
156
- // SOLO agregar TXT record (aquí está tu IP correcta)
131
+ // Agregar TXT record
157
132
  if let txtData = sender.txtRecordData() {
158
133
  let txtRecord = NetService.dictionary(fromTXTRecord: txtData)
159
134
  var txtRecordDict: [String: String] = [:]
160
135
  for (key, value) in txtRecord {
161
136
  if let strValue = String(data: value, encoding: .utf8) {
162
137
  txtRecordDict[key] = strValue
163
- print(" TXT Record retrieved: \(key) = \(strValue)")
164
138
  }
165
139
  }
166
140
  serviceData["txtRecord"] = txtRecordDict
@@ -170,9 +144,7 @@ extension NetworkDiscovery: NetServiceBrowserDelegate {
170
144
  }
171
145
 
172
146
  public func netService(_ sender: NetService, didNotResolve errorDict: [String : NSNumber]) {
173
- print("❌ iOS: Service did NOT resolve")
174
- print(" Service: \(sender.name)")
175
- print(" Error dict: \(errorDict)")
147
+ print("Service did not resolve: \(errorDict)")
176
148
  }
177
149
  }
178
150
 
@@ -2,32 +2,15 @@ import Foundation
2
2
  import Capacitor
3
3
 
4
4
  @objc(NetworkDiscoveryPlugin)
5
- public class NetworkDiscoveryPlugin: CAPPlugin, CAPBridgedPlugin, NetworkDiscoveryDelegate {
6
-
7
- // MARK: - CAPBridgedPlugin Properties
8
- public let identifier = "NetworkDiscoveryPlugin"
9
- public let jsName = "NetworkDiscovery"
10
- public let pluginMethods: [CAPPluginMethod] = [
11
- CAPPluginMethod(name: "startAdvertising", returnType: CAPPluginReturnPromise),
12
- CAPPluginMethod(name: "stopAdvertising", returnType: CAPPluginReturnPromise),
13
- CAPPluginMethod(name: "startDiscovery", returnType: CAPPluginReturnPromise),
14
- CAPPluginMethod(name: "stopDiscovery", returnType: CAPPluginReturnPromise)
15
- ]
16
-
17
- // MARK: - Properties
5
+ public class NetworkDiscoveryPlugin: CAPPlugin, NetworkDiscoveryDelegate {
18
6
  private var implementation: NetworkDiscovery?
19
7
 
20
- // MARK: - Lifecycle
21
- public override func load() {
22
- print("✅ NetworkDiscoveryPlugin: Plugin loaded")
8
+ override public func load() {
23
9
  implementation = NetworkDiscovery()
24
10
  implementation?.delegate = self
25
11
  }
26
12
 
27
- // MARK: - Plugin Methods
28
13
  @objc func startAdvertising(_ call: CAPPluginCall) {
29
- print("📞 NetworkDiscoveryPlugin: startAdvertising() called")
30
-
31
14
  guard let serviceName = call.getString("serviceName"),
32
15
  let serviceType = call.getString("serviceType"),
33
16
  let port = call.getInt("port") else {
@@ -37,8 +20,6 @@ public class NetworkDiscoveryPlugin: CAPPlugin, CAPBridgedPlugin, NetworkDiscove
37
20
 
38
21
  let txtRecord = call.getObject("txtRecord") as? [String: String]
39
22
 
40
- print("📡 NetworkDiscoveryPlugin: Starting advertising - \(serviceName)")
41
-
42
23
  implementation?.startAdvertising(
43
24
  serviceName: serviceName,
44
25
  serviceType: serviceType,
@@ -50,14 +31,11 @@ public class NetworkDiscoveryPlugin: CAPPlugin, CAPBridgedPlugin, NetworkDiscove
50
31
  }
51
32
 
52
33
  @objc func stopAdvertising(_ call: CAPPluginCall) {
53
- print("📞 NetworkDiscoveryPlugin: stopAdvertising() called")
54
34
  implementation?.stopAdvertising()
55
35
  call.resolve(["success": true])
56
36
  }
57
37
 
58
38
  @objc func startDiscovery(_ call: CAPPluginCall) {
59
- print("📞 NetworkDiscoveryPlugin: startDiscovery() called")
60
-
61
39
  guard let serviceType = call.getString("serviceType") else {
62
40
  call.reject("Missing serviceType parameter")
63
41
  return
@@ -65,40 +43,33 @@ public class NetworkDiscoveryPlugin: CAPPlugin, CAPBridgedPlugin, NetworkDiscove
65
43
 
66
44
  let domain = call.getString("domain") ?? "local."
67
45
 
68
- print("🔍 NetworkDiscoveryPlugin: Starting discovery for \(serviceType)")
69
-
70
46
  implementation?.startDiscovery(serviceType: serviceType, domain: domain)
71
47
  call.resolve()
72
48
  }
73
49
 
74
50
  @objc func stopDiscovery(_ call: CAPPluginCall) {
75
- print("📞 NetworkDiscoveryPlugin: stopDiscovery() called")
76
51
  implementation?.stopDiscovery()
77
52
  call.resolve(["success": true])
78
53
  }
79
54
 
80
55
  // MARK: - NetworkDiscoveryDelegate
81
56
  public func advertisingDidStart() {
82
- print("✅ NetworkDiscoveryPlugin: Advertising started successfully")
57
+ print("Advertising started successfully")
83
58
  }
84
59
 
85
60
  public func advertisingDidFail(error: String) {
86
- print("❌ NetworkDiscoveryPlugin: Advertising failed - \(error)")
61
+ print("Advertising failed: \(error)")
87
62
  }
88
63
 
89
64
  public func serviceFound(serviceData: [String : Any]) {
90
- print("📨 NetworkDiscoveryPlugin: Service found, notifying listeners")
91
- print(" Service data: \(serviceData)")
92
65
  notifyListeners("serviceFound", data: serviceData)
93
66
  }
94
67
 
95
68
  public func serviceLost(serviceData: [String : Any]) {
96
- print("📨 NetworkDiscoveryPlugin: Service lost, notifying listeners")
97
- print(" Service data: \(serviceData)")
98
69
  notifyListeners("serviceLost", data: serviceData)
99
70
  }
100
71
 
101
72
  public func discoveryDidFail(error: String) {
102
- print("❌ NetworkDiscoveryPlugin: Discovery failed - \(error)")
73
+ print("Discovery failed: \(error)")
103
74
  }
104
75
  }