Skip to content
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ $connectionSettings = (new \PhpMqtt\Client\ConnectionSettings)
// This setting is only relevant if setReconnectAutomatically() is set to true.
->setMaxReconnectAttempts(3)

// Defines the delay between reconnect attempts in milliseconds.
// This setting is only relevant if setReconnectAutomatically() is set to true.
->setDelayBetweenReconnectAttempts(0)

// The keep alive interval is the number of seconds the client will wait without sending a message
// until it sends a keep alive signal (ping) to the broker. The value cannot be less than 1 second
// and may not be higher than 65535 seconds. A reasonable value is 10 seconds (the default).
Expand Down
4 changes: 4 additions & 0 deletions src/Concerns/ValidatesConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ protected function ensureConnectionSettingsAreValid(ConnectionSettings $settings
throw new ConfigurationInvalidException('The maximum reconnect attempts cannot be fewer than 1.');
}

if ($settings->getDelayBetweenReconnectAttempts() < 0) {
throw new ConfigurationInvalidException('The delay between reconnect attempts cannot be lower than 0.');
}

if ($settings->getUsername() !== null && trim($settings->getUsername()) === '') {
throw new ConfigurationInvalidException('The username may not consist of white space only.');
}
Expand Down
25 changes: 25 additions & 0 deletions src/ConnectionSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class ConnectionSettings
private int $keepAliveInterval = 10;
private bool $reconnectAutomatically = false;
private int $maxReconnectAttempts = 3;
private int $delayBetweenReconnectAttempts = 0;
private ?string $lastWillTopic = null;
private ?string $lastWillMessage = null;
private int $lastWillQualityOfService = 0;
Expand Down Expand Up @@ -226,6 +227,30 @@ public function getMaxReconnectAttempts(): int
return $this->maxReconnectAttempts;
}

/**
* Defines the delay between reconnect attempts in milliseconds.
* This setting is only relevant if {@see setReconnectAutomatically()} is set to true.
*
* @param int $delayBetweenReconnectAttempts
* @return ConnectionSettings
*/
public function setDelayBetweenReconnectAttempts(int $delayBetweenReconnectAttempts): ConnectionSettings
{
$copy = clone $this;

$copy->delayBetweenReconnectAttempts = $delayBetweenReconnectAttempts;

return $copy;
}

/**
* @return int
*/
public function getDelayBetweenReconnectAttempts(): int
{
return $this->delayBetweenReconnectAttempts;
}

/**
* If the broker should publish a last will message in the name of the client when the client
* disconnects abruptly, this setting defines the topic on which the message will be published.
Expand Down
7 changes: 6 additions & 1 deletion src/MqttClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,8 @@ protected function performConnectionHandshake(bool $useCleanSession = false): vo
*/
protected function reconnect(): void
{
$maxReconnectAttempts = $this->settings->getMaxReconnectAttempts();
$maxReconnectAttempts = $this->settings->getMaxReconnectAttempts();
$delayBetweenReconnectAttempts = $this->settings->getDelayBetweenReconnectAttempts();

for ($i = 1; $i <= $maxReconnectAttempts; $i++) {
try {
Expand All @@ -420,6 +421,10 @@ protected function reconnect(): void
if ($i === $maxReconnectAttempts) {
throw $e;
}

if ($delayBetweenReconnectAttempts > 0) {
usleep($delayBetweenReconnectAttempts * 1000);
}
}
}
}
Expand Down