Skip to content

Commit ccd0262

Browse files
feat(backgroundmode): app-93 MQTT Client reconnect Android
1 parent 6b5506b commit ccd0262

File tree

4 files changed

+42
-6
lines changed

4 files changed

+42
-6
lines changed

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

+11-5
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ class RNMqttClient(reactContext: ReactApplicationContext)
252252
* - `clientId`: {`String`} Client ID of the device.
253253
* - `host`: {`String`} Host name of the MQTT broker.
254254
* - `port`: {`int`} Port of the MQTT broker.
255+
* - `reconnect`: {`Boolean`} Reconnect if connection is lost.
255256
*
256257
* @param params
257258
*
@@ -304,7 +305,9 @@ class RNMqttClient(reactContext: ReactApplicationContext)
304305
override fun connectionLost(cause: Throwable?) {
305306
Log.d(NAME, "connectionLost", cause)
306307
this@RNMqttClient.notifyError("ERROR_CONNECTION", cause)
307-
this@RNMqttClient.notifyEvent("disconnected", null)
308+
if(!parsedParams.reconnect) {
309+
this@RNMqttClient.notifyEvent("disconnected", null)
310+
}
308311
}
309312

310313
override fun deliveryComplete(token: IMqttDeliveryToken) {
@@ -340,8 +343,9 @@ class RNMqttClient(reactContext: ReactApplicationContext)
340343
}
341344
})
342345
val connectOptions = MqttConnectOptions()
343-
connectOptions.setSocketFactory(socketFactory)
344-
connectOptions.setCleanSession(true)
346+
connectOptions.socketFactory = socketFactory
347+
connectOptions.isCleanSession = true
348+
connectOptions.isAutomaticReconnect = parsedParams.reconnect
345349
Log.d(NAME, "connecting to the broker")
346350
val token = client.connect(connectOptions)
347351
token.setActionCallback(object: IMqttActionListener {
@@ -549,15 +553,17 @@ class RNMqttClient(reactContext: ReactApplicationContext)
549553
private class ConnectionParameters(
550554
val host: String,
551555
val port: Int,
552-
val clientId: String
556+
val clientId: String,
557+
val reconnect: Boolean
553558
) {
554559
companion object {
555560
// Parses a given object from JavaScript.
556561
fun parseReadableMap(params: ReadableMap): ConnectionParameters {
557562
return ConnectionParameters(
558563
host=params.getRequiredString("host"),
559564
port=params.getRequiredInt("port"),
560-
clientId=params.getRequiredString("clientId")
565+
clientId=params.getRequiredString("clientId"),
566+
reconnect=params.getRequiredBoolean("reconnect")
561567
)
562568
}
563569
}

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

+29
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,32 @@ fun ReadableMap.getOptionalMap(key: String): ReadableMap? {
118118
}
119119
return this.getMap(key)
120120
}
121+
122+
/**
123+
* Obtains an optional Boolean value from a given `ReadableMap`.
124+
*
125+
* @param key
126+
*
127+
* Key associated with the value to be obtained.
128+
*
129+
* @return
130+
*
131+
* Boolean value associated with `key`.
132+
* `false` if no value is associated with `key`.
133+
*
134+
* @throws IllegalArgumentException
135+
*
136+
* If the value associated with `key` is not a Boolean.
137+
*/
138+
fun ReadableMap.getRequiredBoolean(key: String): Boolean {
139+
if (!this.hasKey(key)) {
140+
return false
141+
}
142+
if (this.getType(key) != ReadableType.Boolean) {
143+
throw IllegalArgumentException(
144+
"$key must be associated with a Boolean" +
145+
" but ${this.getType(key)} was given"
146+
)
147+
}
148+
return this.getBoolean(key)
149+
}

react-native-mqtt-client.podspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ Pod::Spec.new do |s|
1818

1919

2020
s.dependency "React"
21-
s.dependency "CocoaMQTT"
21+
s.dependency "CocoaMQTT", "~> 2.1.1"
2222
end

src/index.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ export type ConnectionParameters = {
287287
host: string;
288288
port: string;
289289
clientId: string;
290+
reconnect: boolean;
290291
};
291292

292293
const defaultInstance = new MqttClient();

0 commit comments

Comments
 (0)