From fe0cb883e708536ba6ac08f2a5edabb415a9ce8b Mon Sep 17 00:00:00 2001 From: "iVampireSP.com" Date: Wed, 23 Nov 2022 05:26:38 +0800 Subject: [PATCH] Add auto reconnect support (#31) * Update mqtt-client.php * Update ConnectionManager.php * Update mqtt-client.php * Update mqtt-client.php * Update mqtt-client.php * Update ConnectionManager.php * Update mqtt-client.php * Add and normalize auto-reconnect settings * Add .gitkeep * Remove .gitkeep Co-authored-by: Marvin Mall --- config/mqtt-client.php | 8 ++++++++ src/ConnectionManager.php | 5 ++++- 2 files changed, 12 insertions(+), 1 deletion(-) 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)); } }