@@ -252,6 +252,7 @@ class RNMqttClient(reactContext: ReactApplicationContext)
252
252
* - `clientId`: {`String`} Client ID of the device.
253
253
* - `host`: {`String`} Host name of the MQTT broker.
254
254
* - `port`: {`int`} Port of the MQTT broker.
255
+ * - `reconnect`: {`Boolean`} Reconnect if connection is lost.
255
256
*
256
257
* @param params
257
258
*
@@ -304,7 +305,9 @@ class RNMqttClient(reactContext: ReactApplicationContext)
304
305
override fun connectionLost (cause : Throwable ? ) {
305
306
Log .d(NAME , " connectionLost" , cause)
306
307
this @RNMqttClient.notifyError(" ERROR_CONNECTION" , cause)
307
- this @RNMqttClient.notifyEvent(" disconnected" , null )
308
+ if (! parsedParams.reconnect) {
309
+ this @RNMqttClient.notifyEvent(" disconnected" , null )
310
+ }
308
311
}
309
312
310
313
override fun deliveryComplete (token : IMqttDeliveryToken ) {
@@ -340,8 +343,9 @@ class RNMqttClient(reactContext: ReactApplicationContext)
340
343
}
341
344
})
342
345
val connectOptions = MqttConnectOptions ()
343
- connectOptions.setSocketFactory(socketFactory)
344
- connectOptions.setCleanSession(true )
346
+ connectOptions.socketFactory = socketFactory
347
+ connectOptions.isCleanSession = true
348
+ connectOptions.isAutomaticReconnect = parsedParams.reconnect
345
349
Log .d(NAME , " connecting to the broker" )
346
350
val token = client.connect(connectOptions)
347
351
token.setActionCallback(object : IMqttActionListener {
@@ -549,15 +553,17 @@ class RNMqttClient(reactContext: ReactApplicationContext)
549
553
private class ConnectionParameters (
550
554
val host : String ,
551
555
val port : Int ,
552
- val clientId : String
556
+ val clientId : String ,
557
+ val reconnect : Boolean
553
558
) {
554
559
companion object {
555
560
// Parses a given object from JavaScript.
556
561
fun parseReadableMap (params : ReadableMap ): ConnectionParameters {
557
562
return ConnectionParameters (
558
563
host= params.getRequiredString(" host" ),
559
564
port= params.getRequiredInt(" port" ),
560
- clientId= params.getRequiredString(" clientId" )
565
+ clientId= params.getRequiredString(" clientId" ),
566
+ reconnect= params.getRequiredBoolean(" reconnect" )
561
567
)
562
568
}
563
569
}
0 commit comments