Skip to content

fix(phone-as-device): app-478 app-479 fix NPE client is non connected #10

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

Merged
merged 1 commit into from
Jul 13, 2023
Merged
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
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,14 @@ To handle messages in the subscribed topic, you have to handle a [`receive-messa
MqttClient.disconnect();
```

### Check if client is connected to an MQTT Broker

`MqttClient.isConnected` checks if client is connected to an MQTT Broker.

```js
MqttClient.isConnected();
```

### Loading an identity

An identity stored in a device specific key store by `MqttClient.setIdentity` may be loaded by `MqttClient.loadIdentity`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,31 @@ class RNMqttClient(reactContext: ReactApplicationContext)
}
}

/**
* Determines if this client is currently connected to the server.
* Returns: true if connected, false otherwise.
*
* @param promise
*
* Resolved when check connection has done.
*/
@ReactMethod
fun isConnected(promise: Promise) {
val client = this.client
if (client == null) {
promise.resolve(false)
return
}
try {
val isClientConnected = client.isConnected
promise.resolve(isClientConnected)
} catch (e: Exception) {
Log.e(NAME, "failed to check connection", e)
promise.reject("ERROR_CHECK_CONNECTION", e)
return
}
}

// Notifies a `got-error` event.
private fun notifyError(code: String, cause: Throwable?) {
val params = Arguments.createMap()
Expand Down
2 changes: 2 additions & 0 deletions ios/MqttClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ @interface RCT_EXTERN_MODULE(MqttClient, NSObject)

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

RCT_EXTERN_METHOD(isConnected:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)

RCT_EXTERN_METHOD(disconnect)

RCT_EXTERN_METHOD(publish:(NSString*)topic payload:(NSArray*)payload resolve:(RCTPromiseResolveBlock)resolve reject:(RCTPromiseRejectBlock)reject)
Expand Down
25 changes: 24 additions & 1 deletion ios/MqttClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,30 @@ class MqttClient : RCTEventEmitter {
_ = self.client!.connect()
resolve(nil)
}


@objc(isConnected:reject:)
func isConnected(resolve: RCTPromiseResolveBlock, reject: RCTPromiseRejectBlock) -> Void
{
os_log("MqttClient: isConnected")
guard let client = self.client else {
resolve(false)
return
}
var isConnected: Bool
let connectionState = client.connState
switch connectionState {
case .connected:
isConnected = true
case .connecting:
isConnected = false
case .disconnected:
isConnected = false
default:
isConnected = false
}
resolve(isConnected)
}

@objc(disconnect)
func disconnect() -> Void {
os_log("MqttClient: disconnecting")
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-mqtt-client",
"version": "0.2.0",
"version": "0.2.1",
"description": "MQTT client for React Native",
"main": "lib/commonjs/index",
"module": "lib/module/index",
Expand Down
13 changes: 13 additions & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,19 @@ export class MqttClient {
return MqttClientImpl.subscribe(topic);
}

/**
* Determines if this client is currently connected to the server
*
* @function isConnected
*
* @return {Promise<boolean>}
*
* Resolved when check connection has done.
*/
isConnected(): Promise<boolean> {
return MqttClientImpl.isConnected();
}

/**
* Listens for a given event from this client.
*
Expand Down