Skip to content

Commit d02f9d5

Browse files
Merge pull request #10 from arduino/fix/APP-478-479-isconnected
fix(phone-as-device): app-478 app-479 fix NPE client is non connected
2 parents c745403 + 1151263 commit d02f9d5

File tree

6 files changed

+73
-2
lines changed

6 files changed

+73
-2
lines changed

README.md

+8
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,14 @@ To handle messages in the subscribed topic, you have to handle a [`receive-messa
128128
MqttClient.disconnect();
129129
```
130130

131+
### Check if client is connected to an MQTT Broker
132+
133+
`MqttClient.isConnected` checks if client is connected to an MQTT Broker.
134+
135+
```js
136+
MqttClient.isConnected();
137+
```
138+
131139
### Loading an identity
132140

133141
An identity stored in a device specific key store by `MqttClient.setIdentity` may be loaded by `MqttClient.loadIdentity`.

android/src/main/java/com/github/emotokcak/reactnative/mqtt/RNMqttClient.kt

+25
Original file line numberDiff line numberDiff line change
@@ -537,6 +537,31 @@ class RNMqttClient(reactContext: ReactApplicationContext)
537537
}
538538
}
539539

540+
/**
541+
* Determines if this client is currently connected to the server.
542+
* Returns: true if connected, false otherwise.
543+
*
544+
* @param promise
545+
*
546+
* Resolved when check connection has done.
547+
*/
548+
@ReactMethod
549+
fun isConnected(promise: Promise) {
550+
val client = this.client
551+
if (client == null) {
552+
promise.resolve(false)
553+
return
554+
}
555+
try {
556+
val isClientConnected = client.isConnected
557+
promise.resolve(isClientConnected)
558+
} catch (e: Exception) {
559+
Log.e(NAME, "failed to check connection", e)
560+
promise.reject("ERROR_CHECK_CONNECTION", e)
561+
return
562+
}
563+
}
564+
540565
// Notifies a `got-error` event.
541566
private fun notifyError(code: String, cause: Throwable?) {
542567
val params = Arguments.createMap()

ios/MqttClient.m

+2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ @interface RCT_EXTERN_MODULE(MqttClient, NSObject)
1212

1313
RCT_EXTERN_METHOD(connect:(NSDictionary*)params resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
1414

15+
RCT_EXTERN_METHOD(isConnected:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
16+
1517
RCT_EXTERN_METHOD(disconnect)
1618

1719
RCT_EXTERN_METHOD(publish:(NSString*)topic payload:(NSArray*)payload resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)

ios/MqttClient.swift

+24-1
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,30 @@ class MqttClient : RCTEventEmitter {
324324
_ = self.client!.connect()
325325
resolve(nil)
326326
}
327-
327+
328+
@objc(isConnected:reject:)
329+
func isConnected(resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void
330+
{
331+
os_log("MqttClient: isConnected")
332+
guard let client = self.client else {
333+
resolve(false)
334+
return
335+
}
336+
var isConnected: Bool
337+
let connectionState = client.connState
338+
switch connectionState {
339+
case .connected:
340+
isConnected = true
341+
case .connecting:
342+
isConnected = false
343+
case .disconnected:
344+
isConnected = false
345+
default:
346+
isConnected = false
347+
}
348+
resolve(isConnected)
349+
}
350+
328351
@objc(disconnect)
329352
func disconnect() -> Void {
330353
os_log("MqttClient: disconnecting")

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-mqtt-client",
3-
"version": "0.2.0",
3+
"version": "0.2.1",
44
"description": "MQTT client for React Native",
55
"main": "lib/commonjs/index",
66
"module": "lib/module/index",

src/index.tsx

+13
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,19 @@ export class MqttClient {
182182
return MqttClientImpl.subscribe(topic);
183183
}
184184

185+
/**
186+
* Determines if this client is currently connected to the server
187+
*
188+
* @function isConnected
189+
*
190+
* @return {Promise<boolean>}
191+
*
192+
* Resolved when check connection has done.
193+
*/
194+
isConnected(): Promise<boolean> {
195+
return MqttClientImpl.isConnected();
196+
}
197+
185198
/**
186199
* Listens for a given event from this client.
187200
*

0 commit comments

Comments
 (0)