@inthepocket/react-native-service-discovery 0.2.0
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.
- package/LICENSE +21 -0
- package/README.md +28 -0
- package/android/build.gradle +120 -0
- package/android/gradle.properties +5 -0
- package/android/src/main/AndroidManifest.xml +3 -0
- package/android/src/main/AndroidManifestNew.xml +2 -0
- package/android/src/main/java/com/servicediscovery/ServiceDiscoveryModule.kt +191 -0
- package/android/src/main/java/com/servicediscovery/ServiceDiscoveryPackage.kt +35 -0
- package/android/src/newarch/ServiceDiscoverySpec.kt +7 -0
- package/android/src/oldarch/ServiceDiscoverySpec.kt +15 -0
- package/ios/NSNetService+IPAddress.h +9 -0
- package/ios/NSNetService+IPAddress.m +47 -0
- package/ios/NSNetService+JSON.h +9 -0
- package/ios/NSNetService+JSON.m +22 -0
- package/ios/NSNetService+TXT.h +9 -0
- package/ios/NSNetService+TXT.m +34 -0
- package/ios/ServiceDiscovery.h +14 -0
- package/ios/ServiceDiscovery.mm +225 -0
- package/lib/commonjs/NativeServiceDiscovery.js +9 -0
- package/lib/commonjs/NativeServiceDiscovery.js.map +1 -0
- package/lib/commonjs/index.js +28 -0
- package/lib/commonjs/index.js.map +1 -0
- package/lib/module/NativeServiceDiscovery.js +3 -0
- package/lib/module/NativeServiceDiscovery.js.map +1 -0
- package/lib/module/index.js +19 -0
- package/lib/module/index.js.map +1 -0
- package/lib/typescript/src/NativeServiceDiscovery.d.ts +10 -0
- package/lib/typescript/src/NativeServiceDiscovery.d.ts.map +1 -0
- package/lib/typescript/src/index.d.ts +18 -0
- package/lib/typescript/src/index.d.ts.map +1 -0
- package/package.json +173 -0
- package/react-native-service-discovery.podspec +41 -0
- package/src/NativeServiceDiscovery.ts +11 -0
- package/src/index.ts +56 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 In The Pocket
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
<img src="./banner.png" alt="react-native-service-discovery by In The Pocket" width="100%">
|
|
2
|
+
|
|
3
|
+
### 🌍 mDNS Network Service Discovery in React Native
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/@inthepocket/react-native-service-discovery)
|
|
6
|
+

|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
npm install @inthepocket/react-native-service-discovery
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Usage
|
|
15
|
+
|
|
16
|
+
TODO
|
|
17
|
+
|
|
18
|
+
## Contributing
|
|
19
|
+
|
|
20
|
+
See the [contributing guide](CONTRIBUTING.md) to learn how to contribute to the repository and the development workflow.
|
|
21
|
+
|
|
22
|
+
## License
|
|
23
|
+
|
|
24
|
+
MIT
|
|
25
|
+
|
|
26
|
+
---
|
|
27
|
+
|
|
28
|
+
Made with [create-react-native-library](https://github.com/callstack/react-native-builder-bob)
|
|
@@ -0,0 +1,120 @@
|
|
|
1
|
+
buildscript {
|
|
2
|
+
// Buildscript is evaluated before everything else so we can't use getExtOrDefault
|
|
3
|
+
def kotlin_version = rootProject.ext.has("kotlinVersion") ? rootProject.ext.get("kotlinVersion") : project.properties["ServiceDiscovery_kotlinVersion"]
|
|
4
|
+
|
|
5
|
+
repositories {
|
|
6
|
+
google()
|
|
7
|
+
mavenCentral()
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
dependencies {
|
|
11
|
+
classpath "com.android.tools.build:gradle:7.2.1"
|
|
12
|
+
// noinspection DifferentKotlinGradleVersion
|
|
13
|
+
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
def isNewArchitectureEnabled() {
|
|
18
|
+
return rootProject.hasProperty("newArchEnabled") && rootProject.getProperty("newArchEnabled") == "true"
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
apply plugin: "com.android.library"
|
|
22
|
+
apply plugin: "kotlin-android"
|
|
23
|
+
|
|
24
|
+
if (isNewArchitectureEnabled()) {
|
|
25
|
+
apply plugin: "com.facebook.react"
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
def getExtOrDefault(name) {
|
|
29
|
+
return rootProject.ext.has(name) ? rootProject.ext.get(name) : project.properties["ServiceDiscovery_" + name]
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
def getExtOrIntegerDefault(name) {
|
|
33
|
+
return rootProject.ext.has(name) ? rootProject.ext.get(name) : (project.properties["ServiceDiscovery_" + name]).toInteger()
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
def supportsNamespace() {
|
|
37
|
+
def parsed = com.android.Version.ANDROID_GRADLE_PLUGIN_VERSION.tokenize('.')
|
|
38
|
+
def major = parsed[0].toInteger()
|
|
39
|
+
def minor = parsed[1].toInteger()
|
|
40
|
+
|
|
41
|
+
// Namespace support was added in 7.3.0
|
|
42
|
+
return (major == 7 && minor >= 3) || major >= 8
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
android {
|
|
46
|
+
if (supportsNamespace()) {
|
|
47
|
+
namespace "com.servicediscovery"
|
|
48
|
+
|
|
49
|
+
sourceSets {
|
|
50
|
+
main {
|
|
51
|
+
manifest.srcFile "src/main/AndroidManifestNew.xml"
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
compileSdkVersion getExtOrIntegerDefault("compileSdkVersion")
|
|
57
|
+
|
|
58
|
+
defaultConfig {
|
|
59
|
+
minSdkVersion getExtOrIntegerDefault("minSdkVersion")
|
|
60
|
+
targetSdkVersion getExtOrIntegerDefault("targetSdkVersion")
|
|
61
|
+
buildConfigField "boolean", "IS_NEW_ARCHITECTURE_ENABLED", isNewArchitectureEnabled().toString()
|
|
62
|
+
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
buildFeatures {
|
|
66
|
+
buildConfig true
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
buildTypes {
|
|
70
|
+
release {
|
|
71
|
+
minifyEnabled false
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
lintOptions {
|
|
76
|
+
disable "GradleCompatible"
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
compileOptions {
|
|
80
|
+
sourceCompatibility JavaVersion.VERSION_1_8
|
|
81
|
+
targetCompatibility JavaVersion.VERSION_1_8
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
sourceSets {
|
|
85
|
+
main {
|
|
86
|
+
if (isNewArchitectureEnabled()) {
|
|
87
|
+
java.srcDirs += [
|
|
88
|
+
"src/newarch",
|
|
89
|
+
// This is needed to build Kotlin project with NewArch enabled
|
|
90
|
+
"${project.buildDir}/generated/source/codegen/java"
|
|
91
|
+
]
|
|
92
|
+
} else {
|
|
93
|
+
java.srcDirs += ["src/oldarch"]
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
repositories {
|
|
100
|
+
mavenCentral()
|
|
101
|
+
google()
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
def kotlin_version = getExtOrDefault("kotlinVersion")
|
|
105
|
+
|
|
106
|
+
dependencies {
|
|
107
|
+
// For < 0.71, this will be from the local maven repo
|
|
108
|
+
// For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
|
|
109
|
+
//noinspection GradleDynamicVersion
|
|
110
|
+
implementation "com.facebook.react:react-native:+"
|
|
111
|
+
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (isNewArchitectureEnabled()) {
|
|
115
|
+
react {
|
|
116
|
+
jsRootDir = file("../src/")
|
|
117
|
+
libraryName = "ServiceDiscovery"
|
|
118
|
+
codegenJavaPackageName = "com.servicediscovery"
|
|
119
|
+
}
|
|
120
|
+
}
|
|
@@ -0,0 +1,191 @@
|
|
|
1
|
+
package com.servicediscovery
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import android.net.nsd.NsdManager
|
|
5
|
+
import android.net.nsd.NsdServiceInfo
|
|
6
|
+
import android.util.Log
|
|
7
|
+
import com.facebook.react.bridge.Promise
|
|
8
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
9
|
+
import com.facebook.react.bridge.ReactMethod
|
|
10
|
+
import com.facebook.react.bridge.WritableMap
|
|
11
|
+
import com.facebook.react.bridge.WritableNativeArray
|
|
12
|
+
import com.facebook.react.bridge.WritableNativeMap
|
|
13
|
+
import com.facebook.react.modules.core.DeviceEventManagerModule
|
|
14
|
+
import java.nio.charset.StandardCharsets
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class ServiceDiscoveryModule internal constructor(context: ReactApplicationContext) :
|
|
18
|
+
ServiceDiscoverySpec(context) {
|
|
19
|
+
|
|
20
|
+
private val discoveryListeners: MutableMap<String, NsdManager.DiscoveryListener> = HashMap()
|
|
21
|
+
private val nsdManager: NsdManager =
|
|
22
|
+
reactApplicationContext.getSystemService(Context.NSD_SERVICE) as NsdManager
|
|
23
|
+
|
|
24
|
+
// Keep track of resolved services, because onServiceLost doesn't pass a resolved service
|
|
25
|
+
private val resolvedServices: MutableMap<String, NsdServiceInfo> = HashMap()
|
|
26
|
+
|
|
27
|
+
override fun getName(): String {
|
|
28
|
+
return NAME
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
private fun sendEvent(eventName: String, params: WritableMap?) {
|
|
32
|
+
reactApplicationContext
|
|
33
|
+
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java)
|
|
34
|
+
.emit(eventName, params)
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
@ReactMethod
|
|
38
|
+
override fun addListener(eventName: String) {
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
@ReactMethod
|
|
42
|
+
override fun removeListeners(count: Double) {
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
@ReactMethod
|
|
46
|
+
override fun startSearch(type: String, promise: Promise) {
|
|
47
|
+
try {
|
|
48
|
+
val serviceType = getServiceType(type)
|
|
49
|
+
if (discoveryListeners.containsKey(serviceType)) {
|
|
50
|
+
// Already searching for $serviceType
|
|
51
|
+
promise.resolve(null)
|
|
52
|
+
return
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
val discoveryListener: NsdManager.DiscoveryListener =
|
|
56
|
+
object : NsdManager.DiscoveryListener {
|
|
57
|
+
override fun onDiscoveryStarted(regType: String) {
|
|
58
|
+
// Service discovery started
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
override fun onDiscoveryStopped(regType: String) {
|
|
62
|
+
// Service discovery stopped
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
override fun onServiceFound(service: NsdServiceInfo) {
|
|
66
|
+
try {
|
|
67
|
+
// Service discovery success
|
|
68
|
+
if (service.serviceType == serviceType) {
|
|
69
|
+
// Resolve the service with ad-hoc listener
|
|
70
|
+
nsdManager.resolveService(service, object : NsdManager.ResolveListener {
|
|
71
|
+
override fun onServiceResolved(serviceInfo: NsdServiceInfo) {
|
|
72
|
+
try {
|
|
73
|
+
resolvedServices[serviceInfo.serviceName] = serviceInfo
|
|
74
|
+
sendEvent(SERVICE_FOUND, serviceToMap(serviceInfo))
|
|
75
|
+
} catch (e: Exception) {
|
|
76
|
+
Log.e(NAME, "Error resolving service", e)
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
override fun onResolveFailed(
|
|
81
|
+
serviceInfo: NsdServiceInfo,
|
|
82
|
+
errorCode: Int
|
|
83
|
+
) {
|
|
84
|
+
Log.e(NAME, "Resolve failed: $errorCode")
|
|
85
|
+
}
|
|
86
|
+
})
|
|
87
|
+
}
|
|
88
|
+
} catch (e: Exception) {
|
|
89
|
+
Log.e(NAME, "Error resolving service", e)
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
override fun onServiceLost(serviceInfo: NsdServiceInfo) {
|
|
94
|
+
try {
|
|
95
|
+
// Service lost
|
|
96
|
+
val service = resolvedServices.remove(serviceInfo.serviceName)
|
|
97
|
+
if (service != null) {
|
|
98
|
+
sendEvent(SERVICE_LOST, serviceToMap(service))
|
|
99
|
+
} else {
|
|
100
|
+
// Send unresolved service in case it was lost before it was resolved
|
|
101
|
+
sendEvent(SERVICE_LOST, serviceToMap(serviceInfo))
|
|
102
|
+
}
|
|
103
|
+
} catch (e: Exception) {
|
|
104
|
+
Log.e(NAME, "Error resolving service", e)
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
override fun onStartDiscoveryFailed(serviceType: String, errorCode: Int) {
|
|
109
|
+
tryStopServiceDiscovery(this)
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
override fun onStopDiscoveryFailed(serviceType: String, errorCode: Int) {
|
|
113
|
+
tryStopServiceDiscovery(this)
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
discoveryListeners[serviceType] = discoveryListener
|
|
117
|
+
nsdManager.discoverServices(serviceType, NsdManager.PROTOCOL_DNS_SD, discoveryListener)
|
|
118
|
+
promise.resolve(null)
|
|
119
|
+
return
|
|
120
|
+
} catch (e: Exception) {
|
|
121
|
+
Log.e(NAME, "Error resolving service", e)
|
|
122
|
+
promise.reject(e)
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
@ReactMethod
|
|
127
|
+
override fun stopSearch(type: String, promise: Promise) {
|
|
128
|
+
try {
|
|
129
|
+
resolvedServices.clear()
|
|
130
|
+
val serviceType = getServiceType(type)
|
|
131
|
+
val discoveryListener = discoveryListeners.remove(serviceType)
|
|
132
|
+
if (discoveryListener != null) {
|
|
133
|
+
tryStopServiceDiscovery(discoveryListener)
|
|
134
|
+
}
|
|
135
|
+
} catch (e: Exception) {
|
|
136
|
+
Log.e(NAME, "Error resolving service", e)
|
|
137
|
+
promise.reject(e)
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
private fun tryStopServiceDiscovery(listener: NsdManager.DiscoveryListener) {
|
|
143
|
+
try {
|
|
144
|
+
nsdManager.stopServiceDiscovery(listener)
|
|
145
|
+
} catch (e: Exception) {
|
|
146
|
+
Log.e(NAME, "Error stopping search", e)
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
private fun getServiceType(type: String): String {
|
|
151
|
+
return String.format("_%s._tcp.", type)
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
private fun serviceToMap(serviceInfo: NsdServiceInfo): WritableMap {
|
|
155
|
+
val service: WritableMap = WritableNativeMap()
|
|
156
|
+
try {
|
|
157
|
+
service.putString("name", serviceInfo.serviceName)
|
|
158
|
+
val serviceType = serviceInfo.serviceType.removePrefix(".");
|
|
159
|
+
service.putString("type", "$serviceType.")
|
|
160
|
+
service.putString("domain", "local.")
|
|
161
|
+
service.putInt("port", serviceInfo.port)
|
|
162
|
+
|
|
163
|
+
val host = serviceInfo.host
|
|
164
|
+
if (host != null) {
|
|
165
|
+
service.putString("hostName", host.canonicalHostName)
|
|
166
|
+
service.putArray("addresses", WritableNativeArray().apply {
|
|
167
|
+
pushString(host.hostAddress)
|
|
168
|
+
})
|
|
169
|
+
} else {
|
|
170
|
+
service.putString("hostName", null)
|
|
171
|
+
service.putArray("addresses", WritableNativeArray())
|
|
172
|
+
}
|
|
173
|
+
val txt: WritableMap = WritableNativeMap()
|
|
174
|
+
for ((key, value) in serviceInfo.attributes ?: emptyMap()) {
|
|
175
|
+
if (value != null) {
|
|
176
|
+
txt.putString(key, String(value, StandardCharsets.UTF_8))
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
service.putMap("txt", txt)
|
|
180
|
+
} catch (e: Exception) {
|
|
181
|
+
Log.e(NAME, "Error stopping search", e)
|
|
182
|
+
}
|
|
183
|
+
return service
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
companion object {
|
|
187
|
+
private const val SERVICE_FOUND = "serviceFound"
|
|
188
|
+
private const val SERVICE_LOST = "serviceLost"
|
|
189
|
+
const val NAME = "ServiceDiscovery"
|
|
190
|
+
}
|
|
191
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
package com.servicediscovery
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.TurboReactPackage
|
|
4
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
5
|
+
import com.facebook.react.bridge.NativeModule
|
|
6
|
+
import com.facebook.react.module.model.ReactModuleInfoProvider
|
|
7
|
+
import com.facebook.react.module.model.ReactModuleInfo
|
|
8
|
+
import java.util.HashMap
|
|
9
|
+
|
|
10
|
+
class ServiceDiscoveryPackage : TurboReactPackage() {
|
|
11
|
+
override fun getModule(name: String, reactContext: ReactApplicationContext): NativeModule? {
|
|
12
|
+
return if (name == ServiceDiscoveryModule.NAME) {
|
|
13
|
+
ServiceDiscoveryModule(reactContext)
|
|
14
|
+
} else {
|
|
15
|
+
null
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
override fun getReactModuleInfoProvider(): ReactModuleInfoProvider {
|
|
20
|
+
return ReactModuleInfoProvider {
|
|
21
|
+
val moduleInfos: MutableMap<String, ReactModuleInfo> = HashMap()
|
|
22
|
+
val isTurboModule: Boolean = BuildConfig.IS_NEW_ARCHITECTURE_ENABLED
|
|
23
|
+
moduleInfos[ServiceDiscoveryModule.NAME] = ReactModuleInfo(
|
|
24
|
+
ServiceDiscoveryModule.NAME,
|
|
25
|
+
ServiceDiscoveryModule.NAME,
|
|
26
|
+
false, // canOverrideExistingModule
|
|
27
|
+
false, // needsEagerInit
|
|
28
|
+
true, // hasConstants
|
|
29
|
+
false, // isCxxModule
|
|
30
|
+
isTurboModule // isTurboModule
|
|
31
|
+
)
|
|
32
|
+
moduleInfos
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
package com.servicediscovery
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.bridge.ReactApplicationContext
|
|
4
|
+
import com.facebook.react.bridge.ReactContextBaseJavaModule
|
|
5
|
+
import com.facebook.react.bridge.Promise
|
|
6
|
+
|
|
7
|
+
abstract class ServiceDiscoverySpec internal constructor(context: ReactApplicationContext) :
|
|
8
|
+
ReactContextBaseJavaModule(context) {
|
|
9
|
+
|
|
10
|
+
abstract fun startSearch(type: String, promise: Promise)
|
|
11
|
+
abstract fun stopSearch(type: String, promise: Promise)
|
|
12
|
+
abstract fun addListener(eventName: String)
|
|
13
|
+
abstract fun removeListeners(count: Double)
|
|
14
|
+
|
|
15
|
+
}
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
// NSNetService+IPAddress.m
|
|
2
|
+
|
|
3
|
+
#include <arpa/inet.h>
|
|
4
|
+
|
|
5
|
+
#import "NSNetService+IPAddress.h"
|
|
6
|
+
|
|
7
|
+
@implementation NSNetService (IPAddress)
|
|
8
|
+
|
|
9
|
+
- (NSArray *)ipAddresses
|
|
10
|
+
{
|
|
11
|
+
NSMutableArray *addresses = [NSMutableArray array];
|
|
12
|
+
|
|
13
|
+
// Loop through the addresses associated with the service
|
|
14
|
+
for (NSData *data in self.addresses) {
|
|
15
|
+
// Buffer to hold the address string
|
|
16
|
+
char addressBuffer[INET6_ADDRSTRLEN];
|
|
17
|
+
|
|
18
|
+
// Define a union to handle both IPv4 and IPv6 addresses
|
|
19
|
+
typedef union {
|
|
20
|
+
struct sockaddr sa;
|
|
21
|
+
struct sockaddr_in ipv4;
|
|
22
|
+
struct sockaddr_in6 ipv6;
|
|
23
|
+
} ip_socket_address;
|
|
24
|
+
|
|
25
|
+
ip_socket_address *socketAddress = (ip_socket_address *)[data bytes];
|
|
26
|
+
|
|
27
|
+
// Check if the socket address is valid and is either IPv4 or IPv6
|
|
28
|
+
if (socketAddress && (socketAddress->sa.sa_family == AF_INET || socketAddress->sa.sa_family == AF_INET6)) {
|
|
29
|
+
// Convert the binary address to a human-readable string
|
|
30
|
+
const char *addressStr = inet_ntop(
|
|
31
|
+
socketAddress->sa.sa_family,
|
|
32
|
+
(socketAddress->sa.sa_family == AF_INET ? (void *)&(socketAddress->ipv4.sin_addr) : (void *)&(socketAddress->ipv6.sin6_addr)),
|
|
33
|
+
addressBuffer,
|
|
34
|
+
sizeof(addressBuffer)
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
// If the conversion was successful, add the address to the array
|
|
38
|
+
if (addressStr) {
|
|
39
|
+
[addresses addObject:[NSString stringWithFormat:@"%s", addressStr]];
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
return addresses;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
@end
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
// NSNetService+JSON.m
|
|
2
|
+
|
|
3
|
+
#import "NSNetService+JSON.h"
|
|
4
|
+
#import "NSNetService+TXT.h"
|
|
5
|
+
#import "NSNetService+IPAddress.h"
|
|
6
|
+
|
|
7
|
+
@implementation NSNetService (JSON)
|
|
8
|
+
|
|
9
|
+
- (NSDictionary<NSString *, id> *)toJson
|
|
10
|
+
{
|
|
11
|
+
return @{
|
|
12
|
+
@"name": self.name,
|
|
13
|
+
@"type": self.type,
|
|
14
|
+
@"domain": self.domain,
|
|
15
|
+
@"hostName": self.hostName ?: [NSNull null],
|
|
16
|
+
@"port": @(self.port),
|
|
17
|
+
@"txt": [self txtRecord],
|
|
18
|
+
@"addresses": [self ipAddresses],
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
@end
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
// NSNetService+TXT.m
|
|
2
|
+
|
|
3
|
+
#import "NSNetService+TXT.h"
|
|
4
|
+
|
|
5
|
+
@implementation NSNetService (TXT)
|
|
6
|
+
|
|
7
|
+
- (NSDictionary<NSString *, id> *)txtRecord
|
|
8
|
+
{
|
|
9
|
+
// Retrieve the TXT record data associated with the service
|
|
10
|
+
NSDictionary<NSString *, NSData *> *txtDict = [NSNetService dictionaryFromTXTRecordData:self.TXTRecordData];
|
|
11
|
+
|
|
12
|
+
// Dictionary to hold the converted TXT record
|
|
13
|
+
NSMutableDictionary<NSString *, id> *txtRecord = [NSMutableDictionary dictionary];
|
|
14
|
+
|
|
15
|
+
// Loop through the TXT record dictionary
|
|
16
|
+
for (NSString *key in txtDict) {
|
|
17
|
+
// Extract each key-value pair from the TXT record
|
|
18
|
+
NSData *dataValue = txtDict[key];
|
|
19
|
+
// We need to explitly check for NSData because unlike what the iOS
|
|
20
|
+
// documentation says, the value *can* be an NSNull object
|
|
21
|
+
if ([dataValue isKindOfClass:[NSData class]]) {
|
|
22
|
+
// Add the key-value pair to the converted TXT record dictionary
|
|
23
|
+
NSString *stringValue = [[NSString alloc] initWithData:dataValue encoding:NSUTF8StringEncoding];
|
|
24
|
+
txtRecord[key] = stringValue;
|
|
25
|
+
} else if ([dataValue isKindOfClass:[NSNull class]]) {
|
|
26
|
+
// Skip
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// Convert the mutable dictionary to an immutable NSDictionary
|
|
31
|
+
return [NSDictionary dictionaryWithDictionary:txtRecord];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
@end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
|
|
2
|
+
#import <React/RCTEventEmitter.h>
|
|
3
|
+
|
|
4
|
+
#ifdef RCT_NEW_ARCH_ENABLED
|
|
5
|
+
#import "RNServiceDiscoverySpec.h"
|
|
6
|
+
|
|
7
|
+
@interface ServiceDiscovery : RCTEventEmitter <NativeServiceDiscoverySpec>
|
|
8
|
+
#else
|
|
9
|
+
#import <React/RCTBridgeModule.h>
|
|
10
|
+
|
|
11
|
+
@interface ServiceDiscovery : RCTEventEmitter <RCTBridgeModule>
|
|
12
|
+
#endif
|
|
13
|
+
|
|
14
|
+
@end
|
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
#import "ServiceDiscovery.h"
|
|
2
|
+
#import "NSNetService+JSON.h"
|
|
3
|
+
|
|
4
|
+
@interface ServiceDiscovery () <NSNetServiceBrowserDelegate, NSNetServiceDelegate>
|
|
5
|
+
|
|
6
|
+
@property (nonatomic, strong) NSMutableDictionary<NSString *, NSNetServiceBrowser *> *browsers;
|
|
7
|
+
@property (nonatomic, strong) NSMutableDictionary<NSString *, NSMutableDictionary *> *servicesByType;
|
|
8
|
+
@property (nonatomic, assign) BOOL hasListeners;
|
|
9
|
+
|
|
10
|
+
@end
|
|
11
|
+
|
|
12
|
+
@implementation ServiceDiscovery
|
|
13
|
+
RCT_EXPORT_MODULE()
|
|
14
|
+
|
|
15
|
+
+ (BOOL)requiresMainQueueSetup
|
|
16
|
+
{
|
|
17
|
+
return YES;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
#pragma mark - Lifecycle
|
|
21
|
+
|
|
22
|
+
- (instancetype)init
|
|
23
|
+
{
|
|
24
|
+
self = [super init];
|
|
25
|
+
if (self) {
|
|
26
|
+
// Initialize dictionaries to store browsers and services
|
|
27
|
+
_browsers = [NSMutableDictionary dictionary];
|
|
28
|
+
_servicesByType = [NSMutableDictionary dictionary];
|
|
29
|
+
}
|
|
30
|
+
return self;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
- (void)dealloc
|
|
34
|
+
{
|
|
35
|
+
for (NSNetServiceBrowser *browser in self.browsers.allValues) {
|
|
36
|
+
[browser stop];
|
|
37
|
+
browser.delegate = nil;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
- (NSArray<NSString *> *)supportedEvents
|
|
42
|
+
{
|
|
43
|
+
return @[@"serviceFound", @"serviceLost"];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
- (dispatch_queue_t) methodQueue
|
|
47
|
+
{
|
|
48
|
+
return dispatch_get_main_queue();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
- (void)startObserving
|
|
52
|
+
{
|
|
53
|
+
self.hasListeners = YES;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
- (void)stopObserving
|
|
57
|
+
{
|
|
58
|
+
self.hasListeners = NO;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
#pragma mark - Public API
|
|
62
|
+
|
|
63
|
+
RCT_EXPORT_METHOD(startSearch:(NSString *)type
|
|
64
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
65
|
+
reject:(RCTPromiseRejectBlock)reject)
|
|
66
|
+
{
|
|
67
|
+
NSNetServiceBrowser *browser = self.browsers[type];
|
|
68
|
+
|
|
69
|
+
if (!browser) {
|
|
70
|
+
// Create a new NSNetServiceBrowser instance if not exists
|
|
71
|
+
browser = [[NSNetServiceBrowser alloc] init];
|
|
72
|
+
browser.delegate = self;
|
|
73
|
+
|
|
74
|
+
// Store the browser and associated services in dictionaries
|
|
75
|
+
self.browsers[type] = browser;
|
|
76
|
+
self.servicesByType[type] = [NSMutableDictionary dictionary];
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
// Start searching for services based on type
|
|
80
|
+
NSString *serviceType = [NSString stringWithFormat:@"_%@._tcp.", type];
|
|
81
|
+
[browser searchForServicesOfType:serviceType inDomain:@""]; // Intentionally left blank (instead of limiting to "local.")
|
|
82
|
+
resolve(nil);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
RCT_EXPORT_METHOD(stopSearch:(NSString *)type
|
|
86
|
+
resolve:(RCTPromiseResolveBlock)resolve
|
|
87
|
+
reject:(RCTPromiseRejectBlock)reject)
|
|
88
|
+
{
|
|
89
|
+
NSNetServiceBrowser *browser = self.browsers[type];
|
|
90
|
+
|
|
91
|
+
if (browser) {
|
|
92
|
+
// Stop the browser and associated services when requested
|
|
93
|
+
[browser stop];
|
|
94
|
+
|
|
95
|
+
NSMutableDictionary *services = self.servicesByType[type];
|
|
96
|
+
|
|
97
|
+
for (NSNetService *service in services.allValues) {
|
|
98
|
+
[service stop];
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Remove the browser and associated services from dictionaries
|
|
102
|
+
[services removeAllObjects];
|
|
103
|
+
[self.browsers removeObjectForKey:type];
|
|
104
|
+
[self.servicesByType removeObjectForKey:type];
|
|
105
|
+
}
|
|
106
|
+
resolve(nil);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
#pragma mark - Private methods
|
|
110
|
+
|
|
111
|
+
// Send a React Native event with the service details
|
|
112
|
+
- (void)sendServiceEventWithName:(NSString *)eventName service:(NSNetService *)service
|
|
113
|
+
{
|
|
114
|
+
if (self.hasListeners) {
|
|
115
|
+
[self sendEventWithName:eventName body:[service toJson]];
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
#pragma mark - NSNetServiceBrowserDelegate
|
|
120
|
+
|
|
121
|
+
// Called when a service is found during the search
|
|
122
|
+
- (void)netServiceBrowser:(NSNetServiceBrowser *)browser didFindService:(NSNetService *)service moreComing:(BOOL)moreComing
|
|
123
|
+
{
|
|
124
|
+
if (service == nil) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Use the modified lookup key without the underscore and protocol
|
|
129
|
+
NSString *type = [self typeWithoutUnderscoreAndProtocol:service.type];
|
|
130
|
+
|
|
131
|
+
// Retrieve the services dictionary based on the service type
|
|
132
|
+
NSMutableDictionary *services = self.servicesByType[type];
|
|
133
|
+
|
|
134
|
+
if (services) {
|
|
135
|
+
// Add the service to the dictionary and start resolving its details
|
|
136
|
+
services[service.name] = service;
|
|
137
|
+
service.delegate = self;
|
|
138
|
+
[service resolveWithTimeout:0.0];
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// Called when a service is removed during the search
|
|
143
|
+
- (void)netServiceBrowser:(NSNetServiceBrowser *)browser didRemoveService:(NSNetService *)service moreComing:(BOOL)moreComing
|
|
144
|
+
{
|
|
145
|
+
if (service == nil) {
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
// Stop resolving
|
|
150
|
+
[service stop];
|
|
151
|
+
|
|
152
|
+
// Use the modified lookup key without the underscore and protocol
|
|
153
|
+
NSString *type = [self typeWithoutUnderscoreAndProtocol:service.type];
|
|
154
|
+
|
|
155
|
+
// Retrieve the services dictionary based on the modified service type
|
|
156
|
+
NSMutableDictionary *services = self.servicesByType[type];
|
|
157
|
+
|
|
158
|
+
NSNetService* cachedService = services[service.name];
|
|
159
|
+
if (cachedService) {
|
|
160
|
+
// Send a serviceLost event to React Native
|
|
161
|
+
[self sendServiceEventWithName:@"serviceLost" service:cachedService];
|
|
162
|
+
// Remove the service from the dictionary
|
|
163
|
+
[services removeObjectForKey:cachedService.name];
|
|
164
|
+
} else {
|
|
165
|
+
// Send unresolved service in case it was lost before it was resolved
|
|
166
|
+
[self sendServiceEventWithName:@"serviceLost" service:service];
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
#pragma mark - NSNetServiceDelegate
|
|
171
|
+
|
|
172
|
+
// Called when the details of a service are resolved
|
|
173
|
+
- (void)netServiceDidResolveAddress:(NSNetService *)service
|
|
174
|
+
{
|
|
175
|
+
// Use the modified lookup key without the underscore and protocol
|
|
176
|
+
NSString *type = [self typeWithoutUnderscoreAndProtocol:service.type];
|
|
177
|
+
|
|
178
|
+
// Retrieve the services dictionary based on the service type
|
|
179
|
+
NSMutableDictionary *services = self.servicesByType[type];
|
|
180
|
+
|
|
181
|
+
NSNetService* cachedService = services[service.name];
|
|
182
|
+
if (cachedService) {
|
|
183
|
+
// Send a serviceFound event to React Native
|
|
184
|
+
[self sendServiceEventWithName:@"serviceFound" service:cachedService];
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
#pragma mark - Helpers
|
|
189
|
+
|
|
190
|
+
- (NSString *)typeWithoutUnderscoreAndProtocol:(NSString *)fullType
|
|
191
|
+
{
|
|
192
|
+
// Define a regular expression pattern with hardcoded "tcp" protocol
|
|
193
|
+
NSString *pattern = @"^_(.*?)\\._tcp\\.$";
|
|
194
|
+
|
|
195
|
+
// Create a regular expression object with the defined pattern
|
|
196
|
+
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:0 error:nil];
|
|
197
|
+
|
|
198
|
+
// Find the first match in the fullType using the regular expression
|
|
199
|
+
NSTextCheckingResult *match = [regex firstMatchInString:fullType options:0 range:NSMakeRange(0, fullType.length)];
|
|
200
|
+
|
|
201
|
+
// Check if a match is found and if there are captured groups
|
|
202
|
+
if (match && [match numberOfRanges] > 1) {
|
|
203
|
+
// Extract the captured part of the type without the underscore and protocol
|
|
204
|
+
NSString *typeWithoutUnderscoreAndProtocol = [fullType substringWithRange:[match rangeAtIndex:1]];
|
|
205
|
+
|
|
206
|
+
// Return the modified type
|
|
207
|
+
return typeWithoutUnderscoreAndProtocol;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
// Return the original type if no match or capture is found
|
|
211
|
+
return fullType;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
#pragma mark - New arch
|
|
215
|
+
|
|
216
|
+
// Don't compile this code when we build for the old architecture.
|
|
217
|
+
#ifdef RCT_NEW_ARCH_ENABLED
|
|
218
|
+
- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
|
|
219
|
+
(const facebook::react::ObjCTurboModule::InitParams &)params
|
|
220
|
+
{
|
|
221
|
+
return std::make_shared<facebook::react::NativeServiceDiscoverySpecJSI>(params);
|
|
222
|
+
}
|
|
223
|
+
#endif
|
|
224
|
+
|
|
225
|
+
@end
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.default = void 0;
|
|
7
|
+
var _reactNative = require("react-native");
|
|
8
|
+
var _default = exports.default = _reactNative.TurboModuleRegistry.getEnforcing('ServiceDiscovery');
|
|
9
|
+
//# sourceMappingURL=NativeServiceDiscovery.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_reactNative","require","_default","exports","default","TurboModuleRegistry","getEnforcing"],"sourceRoot":"../../src","sources":["NativeServiceDiscovery.ts"],"mappings":";;;;;;AACA,IAAAA,YAAA,GAAAC,OAAA;AAAmD,IAAAC,QAAA,GAAAC,OAAA,CAAAC,OAAA,GASpCC,gCAAmB,CAACC,YAAY,CAAO,kBAAkB,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.stopSearch = exports.startSearch = exports.addEventListener = void 0;
|
|
7
|
+
var _reactNative = require("react-native");
|
|
8
|
+
const LINKING_ERROR = `The package '@inthepocket/react-native-service-discovery' doesn't seem to be linked. Make sure: \n\n` + _reactNative.Platform.select({
|
|
9
|
+
ios: "- You have run 'pod install'\n",
|
|
10
|
+
default: ''
|
|
11
|
+
}) + '- You rebuilt the app after installing the package\n' + '- You are not using Expo Go\n';
|
|
12
|
+
|
|
13
|
+
// @ts-expect-error
|
|
14
|
+
const isTurboModuleEnabled = global.__turboModuleProxy != null;
|
|
15
|
+
const ServiceDiscoveryModule = isTurboModuleEnabled ? require('./NativeServiceDiscovery').default : _reactNative.NativeModules.ServiceDiscovery;
|
|
16
|
+
const ServiceDiscovery = ServiceDiscoveryModule ? ServiceDiscoveryModule : new Proxy({}, {
|
|
17
|
+
get() {
|
|
18
|
+
throw new Error(LINKING_ERROR);
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
const EventEmitter = new _reactNative.NativeEventEmitter(ServiceDiscovery);
|
|
22
|
+
const addEventListener = (event, listener) => EventEmitter.addListener(event, listener);
|
|
23
|
+
exports.addEventListener = addEventListener;
|
|
24
|
+
const startSearch = serviceType => ServiceDiscovery.startSearch(serviceType);
|
|
25
|
+
exports.startSearch = startSearch;
|
|
26
|
+
const stopSearch = serviceType => ServiceDiscovery.stopSearch(serviceType);
|
|
27
|
+
exports.stopSearch = stopSearch;
|
|
28
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["_reactNative","require","LINKING_ERROR","Platform","select","ios","default","isTurboModuleEnabled","global","__turboModuleProxy","ServiceDiscoveryModule","NativeModules","ServiceDiscovery","Proxy","get","Error","EventEmitter","NativeEventEmitter","addEventListener","event","listener","addListener","exports","startSearch","serviceType","stopSearch"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":";;;;;;AAAA,IAAAA,YAAA,GAAAC,OAAA;AAEA,MAAMC,aAAa,GAChB,sGAAqG,GACtGC,qBAAQ,CAACC,MAAM,CAAC;EAAEC,GAAG,EAAE,gCAAgC;EAAEC,OAAO,EAAE;AAAG,CAAC,CAAC,GACvE,sDAAsD,GACtD,+BAA+B;;AAEjC;AACA,MAAMC,oBAAoB,GAAGC,MAAM,CAACC,kBAAkB,IAAI,IAAI;AAE9D,MAAMC,sBAAsB,GAAGH,oBAAoB,GAC/CN,OAAO,CAAC,0BAA0B,CAAC,CAACK,OAAO,GAC3CK,0BAAa,CAACC,gBAAgB;AAElC,MAAMA,gBAAgB,GAAGF,sBAAsB,GAC3CA,sBAAsB,GACtB,IAAIG,KAAK,CACP,CAAC,CAAC,EACF;EACEC,GAAGA,CAAA,EAAG;IACJ,MAAM,IAAIC,KAAK,CAACb,aAAa,CAAC;EAChC;AACF,CACF,CAAC;AAoBL,MAAMc,YAAY,GAAG,IAAIC,+BAAkB,CAACL,gBAAgB,CAAC;AAEtD,MAAMM,gBAAgB,GAAGA,CAC9BC,KAAmB,EACnBC,QAAyB,KACRJ,YAAY,CAACK,WAAW,CAACF,KAAK,EAAEC,QAAQ,CAAC;AAACE,OAAA,CAAAJ,gBAAA,GAAAA,gBAAA;AAEtD,MAAMK,WAAW,GAAIC,WAAmB,IAC7CZ,gBAAgB,CAACW,WAAW,CAACC,WAAW,CAAC;AAACF,OAAA,CAAAC,WAAA,GAAAA,WAAA;AAErC,MAAME,UAAU,GAAID,WAAmB,IAC5CZ,gBAAgB,CAACa,UAAU,CAACD,WAAW,CAAC;AAACF,OAAA,CAAAG,UAAA,GAAAA,UAAA","ignoreList":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["TurboModuleRegistry","getEnforcing"],"sourceRoot":"../../src","sources":["NativeServiceDiscovery.ts"],"mappings":"AACA,SAASA,mBAAmB,QAAQ,cAAc;AASlD,eAAeA,mBAAmB,CAACC,YAAY,CAAO,kBAAkB,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { NativeEventEmitter, NativeModules, Platform } from 'react-native';
|
|
2
|
+
const LINKING_ERROR = `The package '@inthepocket/react-native-service-discovery' doesn't seem to be linked. Make sure: \n\n` + Platform.select({
|
|
3
|
+
ios: "- You have run 'pod install'\n",
|
|
4
|
+
default: ''
|
|
5
|
+
}) + '- You rebuilt the app after installing the package\n' + '- You are not using Expo Go\n';
|
|
6
|
+
|
|
7
|
+
// @ts-expect-error
|
|
8
|
+
const isTurboModuleEnabled = global.__turboModuleProxy != null;
|
|
9
|
+
const ServiceDiscoveryModule = isTurboModuleEnabled ? require('./NativeServiceDiscovery').default : NativeModules.ServiceDiscovery;
|
|
10
|
+
const ServiceDiscovery = ServiceDiscoveryModule ? ServiceDiscoveryModule : new Proxy({}, {
|
|
11
|
+
get() {
|
|
12
|
+
throw new Error(LINKING_ERROR);
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
const EventEmitter = new NativeEventEmitter(ServiceDiscovery);
|
|
16
|
+
export const addEventListener = (event, listener) => EventEmitter.addListener(event, listener);
|
|
17
|
+
export const startSearch = serviceType => ServiceDiscovery.startSearch(serviceType);
|
|
18
|
+
export const stopSearch = serviceType => ServiceDiscovery.stopSearch(serviceType);
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"names":["NativeEventEmitter","NativeModules","Platform","LINKING_ERROR","select","ios","default","isTurboModuleEnabled","global","__turboModuleProxy","ServiceDiscoveryModule","require","ServiceDiscovery","Proxy","get","Error","EventEmitter","addEventListener","event","listener","addListener","startSearch","serviceType","stopSearch"],"sourceRoot":"../../src","sources":["index.ts"],"mappings":"AAAA,SAASA,kBAAkB,EAAEC,aAAa,EAAEC,QAAQ,QAAQ,cAAc;AAE1E,MAAMC,aAAa,GAChB,sGAAqG,GACtGD,QAAQ,CAACE,MAAM,CAAC;EAAEC,GAAG,EAAE,gCAAgC;EAAEC,OAAO,EAAE;AAAG,CAAC,CAAC,GACvE,sDAAsD,GACtD,+BAA+B;;AAEjC;AACA,MAAMC,oBAAoB,GAAGC,MAAM,CAACC,kBAAkB,IAAI,IAAI;AAE9D,MAAMC,sBAAsB,GAAGH,oBAAoB,GAC/CI,OAAO,CAAC,0BAA0B,CAAC,CAACL,OAAO,GAC3CL,aAAa,CAACW,gBAAgB;AAElC,MAAMA,gBAAgB,GAAGF,sBAAsB,GAC3CA,sBAAsB,GACtB,IAAIG,KAAK,CACP,CAAC,CAAC,EACF;EACEC,GAAGA,CAAA,EAAG;IACJ,MAAM,IAAIC,KAAK,CAACZ,aAAa,CAAC;EAChC;AACF,CACF,CAAC;AAoBL,MAAMa,YAAY,GAAG,IAAIhB,kBAAkB,CAACY,gBAAgB,CAAC;AAE7D,OAAO,MAAMK,gBAAgB,GAAGA,CAC9BC,KAAmB,EACnBC,QAAyB,KACRH,YAAY,CAACI,WAAW,CAACF,KAAK,EAAEC,QAAQ,CAAC;AAE5D,OAAO,MAAME,WAAW,GAAIC,WAAmB,IAC7CV,gBAAgB,CAACS,WAAW,CAACC,WAAW,CAAC;AAE3C,OAAO,MAAMC,UAAU,GAAID,WAAmB,IAC5CV,gBAAgB,CAACW,UAAU,CAACD,WAAW,CAAC","ignoreList":[]}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { TurboModule } from 'react-native';
|
|
2
|
+
export interface Spec extends TurboModule {
|
|
3
|
+
startSearch(serviceType: string): Promise<void>;
|
|
4
|
+
stopSearch(serviceType: string): Promise<void>;
|
|
5
|
+
addListener: (eventType: string) => void;
|
|
6
|
+
removeListeners: (count: number) => void;
|
|
7
|
+
}
|
|
8
|
+
declare const _default: Spec;
|
|
9
|
+
export default _default;
|
|
10
|
+
//# sourceMappingURL=NativeServiceDiscovery.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"NativeServiceDiscovery.d.ts","sourceRoot":"","sources":["../../../src/NativeServiceDiscovery.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAGhD,MAAM,WAAW,IAAK,SAAQ,WAAW;IACvC,WAAW,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAChD,UAAU,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;IAC/C,WAAW,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,IAAI,CAAC;IACzC,eAAe,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;CAC1C;;AAED,wBAA0E"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export interface Service {
|
|
2
|
+
readonly name: string;
|
|
3
|
+
readonly type: string;
|
|
4
|
+
readonly domain: string;
|
|
5
|
+
readonly hostName: string;
|
|
6
|
+
readonly addresses: string[];
|
|
7
|
+
readonly port: number;
|
|
8
|
+
readonly txt: Record<string, string>;
|
|
9
|
+
}
|
|
10
|
+
export type ServiceEvent = 'serviceFound' | 'serviceLost' | 'error';
|
|
11
|
+
export type ServiceCallback = (service: Service) => void;
|
|
12
|
+
export interface Subscription {
|
|
13
|
+
remove: () => void;
|
|
14
|
+
}
|
|
15
|
+
export declare const addEventListener: (event: ServiceEvent, listener: ServiceCallback) => Subscription;
|
|
16
|
+
export declare const startSearch: (serviceType: string) => Promise<void>;
|
|
17
|
+
export declare const stopSearch: (serviceType: string) => Promise<void>;
|
|
18
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/index.ts"],"names":[],"mappings":"AA0BA,MAAM,WAAW,OAAO;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,QAAQ,EAAE,MAAM,CAAC;IAC1B,QAAQ,CAAC,SAAS,EAAE,MAAM,EAAE,CAAC;IAC7B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,GAAG,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACtC;AAED,MAAM,MAAM,YAAY,GAAG,cAAc,GAAG,aAAa,GAAG,OAAO,CAAC;AAEpE,MAAM,MAAM,eAAe,GAAG,CAAC,OAAO,EAAE,OAAO,KAAK,IAAI,CAAC;AAEzD,MAAM,WAAW,YAAY;IAC3B,MAAM,EAAE,MAAM,IAAI,CAAC;CACpB;AAID,eAAO,MAAM,gBAAgB,UACpB,YAAY,YACT,eAAe,KACxB,YAAyD,CAAC;AAE7D,eAAO,MAAM,WAAW,gBAAiB,MAAM,KAAG,QAAQ,IAAI,CACnB,CAAC;AAE5C,eAAO,MAAM,UAAU,gBAAiB,MAAM,KAAG,QAAQ,IAAI,CACnB,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@inthepocket/react-native-service-discovery",
|
|
3
|
+
"version": "0.2.0",
|
|
4
|
+
"description": "mDNS Network Service Discovery in React Native",
|
|
5
|
+
"main": "lib/commonjs/index",
|
|
6
|
+
"module": "lib/module/index",
|
|
7
|
+
"types": "lib/typescript/src/index.d.ts",
|
|
8
|
+
"react-native": "src/index",
|
|
9
|
+
"source": "src/index",
|
|
10
|
+
"files": [
|
|
11
|
+
"src",
|
|
12
|
+
"lib",
|
|
13
|
+
"android",
|
|
14
|
+
"ios",
|
|
15
|
+
"cpp",
|
|
16
|
+
"*.podspec",
|
|
17
|
+
"!ios/build",
|
|
18
|
+
"!android/build",
|
|
19
|
+
"!android/gradle",
|
|
20
|
+
"!android/gradlew",
|
|
21
|
+
"!android/gradlew.bat",
|
|
22
|
+
"!android/local.properties",
|
|
23
|
+
"!**/__tests__",
|
|
24
|
+
"!**/__fixtures__",
|
|
25
|
+
"!**/__mocks__",
|
|
26
|
+
"!**/.*"
|
|
27
|
+
],
|
|
28
|
+
"scripts": {
|
|
29
|
+
"example": "yarn workspace react-native-service-discovery-example",
|
|
30
|
+
"test": "jest",
|
|
31
|
+
"typecheck": "tsc --noEmit",
|
|
32
|
+
"lint": "eslint \"**/*.{js,ts,tsx}\"",
|
|
33
|
+
"clean": "del-cli android/build example/android/build example/android/app/build example/ios/build lib",
|
|
34
|
+
"prepare": "bob build",
|
|
35
|
+
"release": "release-it"
|
|
36
|
+
},
|
|
37
|
+
"keywords": [
|
|
38
|
+
"react-native",
|
|
39
|
+
"zeroconf",
|
|
40
|
+
"bonjour",
|
|
41
|
+
"avahi",
|
|
42
|
+
"network",
|
|
43
|
+
"lan",
|
|
44
|
+
"ios",
|
|
45
|
+
"android",
|
|
46
|
+
"network-discovery",
|
|
47
|
+
"discovery"
|
|
48
|
+
],
|
|
49
|
+
"repository": {
|
|
50
|
+
"type": "git",
|
|
51
|
+
"url": "git+https://github.com/inthepocket/react-native-service-discovery.git"
|
|
52
|
+
},
|
|
53
|
+
"author": "In The Pocket <hello@inthepocket.com> (https://github.com/inthepocket)",
|
|
54
|
+
"license": "MIT",
|
|
55
|
+
"bugs": {
|
|
56
|
+
"url": "https://github.com/inthepocket/react-native-service-discovery/issues"
|
|
57
|
+
},
|
|
58
|
+
"homepage": "https://github.com/inthepocket/react-native-service-discovery#readme",
|
|
59
|
+
"publishConfig": {
|
|
60
|
+
"access": "public",
|
|
61
|
+
"registry": "https://registry.npmjs.org/"
|
|
62
|
+
},
|
|
63
|
+
"devDependencies": {
|
|
64
|
+
"@commitlint/config-conventional": "^17.0.2",
|
|
65
|
+
"@evilmartians/lefthook": "^1.5.0",
|
|
66
|
+
"@react-native/eslint-config": "^0.73.1",
|
|
67
|
+
"@release-it/conventional-changelog": "^5.0.0",
|
|
68
|
+
"@types/jest": "^29.5.5",
|
|
69
|
+
"@types/react": "^18.2.44",
|
|
70
|
+
"commitlint": "^17.0.2",
|
|
71
|
+
"del-cli": "^5.1.0",
|
|
72
|
+
"eslint": "^8.51.0",
|
|
73
|
+
"eslint-config-prettier": "^9.0.0",
|
|
74
|
+
"eslint-plugin-prettier": "^5.0.1",
|
|
75
|
+
"jest": "^29.7.0",
|
|
76
|
+
"prettier": "^3.0.3",
|
|
77
|
+
"react": "18.2.0",
|
|
78
|
+
"react-native": "0.74.0",
|
|
79
|
+
"react-native-builder-bob": "^0.23.2",
|
|
80
|
+
"release-it": "^15.0.0",
|
|
81
|
+
"turbo": "^1.10.7",
|
|
82
|
+
"typescript": "^5.2.2"
|
|
83
|
+
},
|
|
84
|
+
"resolutions": {
|
|
85
|
+
"@types/react": "^18.2.44"
|
|
86
|
+
},
|
|
87
|
+
"peerDependencies": {
|
|
88
|
+
"react": "*",
|
|
89
|
+
"react-native": "*"
|
|
90
|
+
},
|
|
91
|
+
"workspaces": [
|
|
92
|
+
"example"
|
|
93
|
+
],
|
|
94
|
+
"packageManager": "yarn@3.6.1",
|
|
95
|
+
"jest": {
|
|
96
|
+
"preset": "react-native",
|
|
97
|
+
"modulePathIgnorePatterns": [
|
|
98
|
+
"<rootDir>/example/node_modules",
|
|
99
|
+
"<rootDir>/lib/"
|
|
100
|
+
]
|
|
101
|
+
},
|
|
102
|
+
"commitlint": {
|
|
103
|
+
"extends": [
|
|
104
|
+
"@commitlint/config-conventional"
|
|
105
|
+
]
|
|
106
|
+
},
|
|
107
|
+
"release-it": {
|
|
108
|
+
"git": {
|
|
109
|
+
"commitMessage": "chore: release ${version}",
|
|
110
|
+
"tagName": "v${version}"
|
|
111
|
+
},
|
|
112
|
+
"npm": {
|
|
113
|
+
"publish": true
|
|
114
|
+
},
|
|
115
|
+
"github": {
|
|
116
|
+
"release": true
|
|
117
|
+
},
|
|
118
|
+
"plugins": {
|
|
119
|
+
"@release-it/conventional-changelog": {
|
|
120
|
+
"preset": "angular"
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
"eslintConfig": {
|
|
125
|
+
"root": true,
|
|
126
|
+
"extends": [
|
|
127
|
+
"@react-native",
|
|
128
|
+
"prettier"
|
|
129
|
+
],
|
|
130
|
+
"rules": {
|
|
131
|
+
"prettier/prettier": [
|
|
132
|
+
"error",
|
|
133
|
+
{
|
|
134
|
+
"quoteProps": "consistent",
|
|
135
|
+
"singleQuote": true,
|
|
136
|
+
"tabWidth": 2,
|
|
137
|
+
"trailingComma": "es5",
|
|
138
|
+
"useTabs": false
|
|
139
|
+
}
|
|
140
|
+
]
|
|
141
|
+
}
|
|
142
|
+
},
|
|
143
|
+
"eslintIgnore": [
|
|
144
|
+
"node_modules/",
|
|
145
|
+
"lib/"
|
|
146
|
+
],
|
|
147
|
+
"prettier": {
|
|
148
|
+
"quoteProps": "consistent",
|
|
149
|
+
"singleQuote": true,
|
|
150
|
+
"tabWidth": 2,
|
|
151
|
+
"trailingComma": "es5",
|
|
152
|
+
"useTabs": false
|
|
153
|
+
},
|
|
154
|
+
"react-native-builder-bob": {
|
|
155
|
+
"source": "src",
|
|
156
|
+
"output": "lib",
|
|
157
|
+
"targets": [
|
|
158
|
+
"commonjs",
|
|
159
|
+
"module",
|
|
160
|
+
[
|
|
161
|
+
"typescript",
|
|
162
|
+
{
|
|
163
|
+
"project": "tsconfig.build.json"
|
|
164
|
+
}
|
|
165
|
+
]
|
|
166
|
+
]
|
|
167
|
+
},
|
|
168
|
+
"codegenConfig": {
|
|
169
|
+
"name": "RNServiceDiscoverySpec",
|
|
170
|
+
"type": "modules",
|
|
171
|
+
"jsSrcsDir": "src"
|
|
172
|
+
}
|
|
173
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
|
|
3
|
+
package = JSON.parse(File.read(File.join(__dir__, "package.json")))
|
|
4
|
+
folly_compiler_flags = '-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1 -Wno-comma -Wno-shorten-64-to-32'
|
|
5
|
+
|
|
6
|
+
Pod::Spec.new do |s|
|
|
7
|
+
s.name = "react-native-service-discovery"
|
|
8
|
+
s.version = package["version"]
|
|
9
|
+
s.summary = package["description"]
|
|
10
|
+
s.homepage = package["homepage"]
|
|
11
|
+
s.license = package["license"]
|
|
12
|
+
s.authors = package["author"]
|
|
13
|
+
|
|
14
|
+
s.platforms = { :ios => min_ios_version_supported }
|
|
15
|
+
s.source = { :git => "https://github.com/inthepocket/react-native-service-discovery.git", :tag => "#{s.version}" }
|
|
16
|
+
|
|
17
|
+
s.source_files = "ios/**/*.{h,m,mm}"
|
|
18
|
+
|
|
19
|
+
# Use install_modules_dependencies helper to install the dependencies if React Native version >=0.71.0.
|
|
20
|
+
# See https://github.com/facebook/react-native/blob/febf6b7f33fdb4904669f99d795eba4c0f95d7bf/scripts/cocoapods/new_architecture.rb#L79.
|
|
21
|
+
if respond_to?(:install_modules_dependencies, true)
|
|
22
|
+
install_modules_dependencies(s)
|
|
23
|
+
else
|
|
24
|
+
s.dependency "React-Core"
|
|
25
|
+
|
|
26
|
+
# Don't install the dependencies when we run `pod install` in the old architecture.
|
|
27
|
+
if ENV['RCT_NEW_ARCH_ENABLED'] == '1' then
|
|
28
|
+
s.compiler_flags = folly_compiler_flags + " -DRCT_NEW_ARCH_ENABLED=1"
|
|
29
|
+
s.pod_target_xcconfig = {
|
|
30
|
+
"HEADER_SEARCH_PATHS" => "\"$(PODS_ROOT)/boost\"",
|
|
31
|
+
"OTHER_CPLUSPLUSFLAGS" => "-DFOLLY_NO_CONFIG -DFOLLY_MOBILE=1 -DFOLLY_USE_LIBCPP=1",
|
|
32
|
+
"CLANG_CXX_LANGUAGE_STANDARD" => "c++17"
|
|
33
|
+
}
|
|
34
|
+
s.dependency "React-Codegen"
|
|
35
|
+
s.dependency "RCT-Folly"
|
|
36
|
+
s.dependency "RCTRequired"
|
|
37
|
+
s.dependency "RCTTypeSafety"
|
|
38
|
+
s.dependency "ReactCommon/turbomodule/core"
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { TurboModule } from 'react-native';
|
|
2
|
+
import { TurboModuleRegistry } from 'react-native';
|
|
3
|
+
|
|
4
|
+
export interface Spec extends TurboModule {
|
|
5
|
+
startSearch(serviceType: string): Promise<void>;
|
|
6
|
+
stopSearch(serviceType: string): Promise<void>;
|
|
7
|
+
addListener: (eventType: string) => void;
|
|
8
|
+
removeListeners: (count: number) => void;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default TurboModuleRegistry.getEnforcing<Spec>('ServiceDiscovery');
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { NativeEventEmitter, NativeModules, Platform } from 'react-native';
|
|
2
|
+
|
|
3
|
+
const LINKING_ERROR =
|
|
4
|
+
`The package '@inthepocket/react-native-service-discovery' doesn't seem to be linked. Make sure: \n\n` +
|
|
5
|
+
Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
|
|
6
|
+
'- You rebuilt the app after installing the package\n' +
|
|
7
|
+
'- You are not using Expo Go\n';
|
|
8
|
+
|
|
9
|
+
// @ts-expect-error
|
|
10
|
+
const isTurboModuleEnabled = global.__turboModuleProxy != null;
|
|
11
|
+
|
|
12
|
+
const ServiceDiscoveryModule = isTurboModuleEnabled
|
|
13
|
+
? require('./NativeServiceDiscovery').default
|
|
14
|
+
: NativeModules.ServiceDiscovery;
|
|
15
|
+
|
|
16
|
+
const ServiceDiscovery = ServiceDiscoveryModule
|
|
17
|
+
? ServiceDiscoveryModule
|
|
18
|
+
: new Proxy(
|
|
19
|
+
{},
|
|
20
|
+
{
|
|
21
|
+
get() {
|
|
22
|
+
throw new Error(LINKING_ERROR);
|
|
23
|
+
},
|
|
24
|
+
}
|
|
25
|
+
);
|
|
26
|
+
|
|
27
|
+
export interface Service {
|
|
28
|
+
readonly name: string;
|
|
29
|
+
readonly type: string;
|
|
30
|
+
readonly domain: string;
|
|
31
|
+
readonly hostName: string;
|
|
32
|
+
readonly addresses: string[];
|
|
33
|
+
readonly port: number;
|
|
34
|
+
readonly txt: Record<string, string>;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export type ServiceEvent = 'serviceFound' | 'serviceLost' | 'error';
|
|
38
|
+
|
|
39
|
+
export type ServiceCallback = (service: Service) => void;
|
|
40
|
+
|
|
41
|
+
export interface Subscription {
|
|
42
|
+
remove: () => void;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
const EventEmitter = new NativeEventEmitter(ServiceDiscovery);
|
|
46
|
+
|
|
47
|
+
export const addEventListener = (
|
|
48
|
+
event: ServiceEvent,
|
|
49
|
+
listener: ServiceCallback
|
|
50
|
+
): Subscription => EventEmitter.addListener(event, listener);
|
|
51
|
+
|
|
52
|
+
export const startSearch = (serviceType: string): Promise<void> =>
|
|
53
|
+
ServiceDiscovery.startSearch(serviceType);
|
|
54
|
+
|
|
55
|
+
export const stopSearch = (serviceType: string): Promise<void> =>
|
|
56
|
+
ServiceDiscovery.stopSearch(serviceType);
|