diff --git a/config/mqtt-client.php b/config/mqtt-client.php index b6533a9..8077696 100644 --- a/config/mqtt-client.php +++ b/config/mqtt-client.php @@ -101,6 +101,14 @@ return [ // The interval (in seconds) in which the client will send a ping to the broker, // if no other message has been sent. 'keep_alive_interval' => env('MQTT_KEEP_ALIVE_INTERVAL', 10), + + // Additional settings for the optional auto-reconnect. The delay between reconnect attempts is in seconds. + 'auto_reconnect' => [ + 'enabled' => env('MQTT_AUTO_RECONNECT_ENABLED', false), + 'max_reconnect_attempts' => env('MQTT_AUTO_RECONNECT_MAX_RECONNECT_ATTEMPTS', 3), + 'delay_between_reconnect_attempts' => env('MQTT_AUTO_RECONNECT_DELAY_BETWEEN_RECONNECT_ATTEMPTS', 0), + ], + ], ], diff --git a/src/ConnectionManager.php b/src/ConnectionManager.php index b3688bc..0a887d6 100644 --- a/src/ConnectionManager.php +++ b/src/ConnectionManager.php @@ -177,6 +177,9 @@ class ConnectionManager ->setLastWillTopic(Arr::get($config, 'last_will.topic')) ->setLastWillMessage(Arr::get($config, 'last_will.message')) ->setLastWillQualityOfService((int) Arr::get($config, 'last_will.quality_of_service', MqttClient::QOS_AT_MOST_ONCE)) - ->setRetainLastWill((bool) Arr::get($config, 'last_will.retain', false)); + ->setRetainLastWill((bool) Arr::get($config, 'last_will.retain', false)) + ->setReconnectAutomatically((bool) Arr::get($config, 'auto_reconnect.enabled', false)) + ->setMaxReconnectAttempts((int) Arr::get($config, 'auto_reconnect.max_reconnect_attempts', 3)) + ->setDelayBetweenReconnectAttempts((int) Arr::get($config, 'auto_reconnect.delay_between_reconnect_attempts', 0)); } }