Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for automatic reconnects #106

Merged
merged 4 commits into from
May 26, 2022
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,15 @@ $connectionSettings = (new \PhpMqtt\Client\ConnectionSettings)
// of pending messages without acknowledgement. The value cannot be less than 1 second.
->setResendTimeout(10)

// This flag determines whether the client will try to reconnect automatically
// if it notices a disconnect while sending data.
// The setting cannot be used together with the clean session flag.
->setReconnectAutomatically(false)

// Defines the maximum number of reconnect attempts until the client gives up.
// This setting is only relevant if setReconnectAutomatically() is set to true.
->setMaxReconnectAttempts(3)

// 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 @@ -43,6 +43,10 @@ protected function ensureConnectionSettingsAreValid(ConnectionSettings $settings
throw new ConfigurationInvalidException('The keep alive interval must be a value in the range of 1 to 65535 seconds.');
}

if ($settings->getMaxReconnectAttempts() < 1) {
throw new ConfigurationInvalidException('The maximum reconnect attempts cannot be fewer than 1.');
}

if ($settings->getUsername() !== null && trim($settings->getUsername()) === '') {
throw new ConfigurationInvalidException('The username may not consist of white space only.');
}
Expand Down
51 changes: 51 additions & 0 deletions src/ConnectionSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ class ConnectionSettings
private int $socketTimeout = 5;
private int $resendTimeout = 10;
private int $keepAliveInterval = 10;
private bool $reconnectAutomatically = false;
private int $maxReconnectAttempts = 3;
private ?string $lastWillTopic = null;
private ?string $lastWillMessage = null;
private int $lastWillQualityOfService = 0;
Expand Down Expand Up @@ -175,6 +177,55 @@ public function getKeepAliveInterval(): int
return $this->keepAliveInterval;
}

/**
* This flag determines whether the client will try to reconnect automatically,
* if it notices a disconnect while sending data.
* The setting cannot be used together with the clean session flag.
*
* @param bool $reconnectAutomatically
* @return ConnectionSettings
*/
public function setReconnectAutomatically(bool $reconnectAutomatically): ConnectionSettings
{
$copy = clone $this;

$copy->reconnectAutomatically = $reconnectAutomatically;

return $copy;
}

/**
* @return bool
*/
public function shouldReconnectAutomatically(): bool
{
return $this->reconnectAutomatically;
}

/**
* Defines the maximum number of reconnect attempts until the client gives up. This setting
* is only relevant if {@see setReconnectAutomatically()} is set to true.
*
* @param int $maxReconnectAttempts
* @return ConnectionSettings
*/
public function setMaxReconnectAttempts(int $maxReconnectAttempts): ConnectionSettings
{
$copy = clone $this;

$copy->maxReconnectAttempts = $maxReconnectAttempts;

return $copy;
}

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

/**
* 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
Loading