File tree 4 files changed +39
-1
lines changed
4 files changed +39
-1
lines changed Original file line number Diff line number Diff line change @@ -164,6 +164,10 @@ $connectionSettings = (new \PhpMqtt\Client\ConnectionSettings)
164
164
// This setting is only relevant if setReconnectAutomatically() is set to true.
165
165
->setMaxReconnectAttempts(3)
166
166
167
+ // Defines the delay between reconnect attempts in milliseconds.
168
+ // This setting is only relevant if setReconnectAutomatically() is set to true.
169
+ ->setDelayBetweenReconnectAttempts(0)
170
+
167
171
// The keep alive interval is the number of seconds the client will wait without sending a message
168
172
// until it sends a keep alive signal (ping) to the broker. The value cannot be less than 1 second
169
173
// and may not be higher than 65535 seconds. A reasonable value is 10 seconds (the default).
Original file line number Diff line number Diff line change @@ -47,6 +47,10 @@ protected function ensureConnectionSettingsAreValid(ConnectionSettings $settings
47
47
throw new ConfigurationInvalidException ('The maximum reconnect attempts cannot be fewer than 1. ' );
48
48
}
49
49
50
+ if ($ settings ->getDelayBetweenReconnectAttempts () < 0 ) {
51
+ throw new ConfigurationInvalidException ('The delay between reconnect attempts cannot be lower than 0. ' );
52
+ }
53
+
50
54
if ($ settings ->getUsername () !== null && trim ($ settings ->getUsername ()) === '' ) {
51
55
throw new ConfigurationInvalidException ('The username may not consist of white space only. ' );
52
56
}
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ class ConnectionSettings
19
19
private int $ keepAliveInterval = 10 ;
20
20
private bool $ reconnectAutomatically = false ;
21
21
private int $ maxReconnectAttempts = 3 ;
22
+ private int $ delayBetweenReconnectAttempts = 0 ;
22
23
private ?string $ lastWillTopic = null ;
23
24
private ?string $ lastWillMessage = null ;
24
25
private int $ lastWillQualityOfService = 0 ;
@@ -226,6 +227,30 @@ public function getMaxReconnectAttempts(): int
226
227
return $ this ->maxReconnectAttempts ;
227
228
}
228
229
230
+ /**
231
+ * Defines the delay between reconnect attempts in milliseconds.
232
+ * This setting is only relevant if {@see setReconnectAutomatically()} is set to true.
233
+ *
234
+ * @param int $delayBetweenReconnectAttempts
235
+ * @return ConnectionSettings
236
+ */
237
+ public function setDelayBetweenReconnectAttempts (int $ delayBetweenReconnectAttempts ): ConnectionSettings
238
+ {
239
+ $ copy = clone $ this ;
240
+
241
+ $ copy ->delayBetweenReconnectAttempts = $ delayBetweenReconnectAttempts ;
242
+
243
+ return $ copy ;
244
+ }
245
+
246
+ /**
247
+ * @return int
248
+ */
249
+ public function getDelayBetweenReconnectAttempts (): int
250
+ {
251
+ return $ this ->delayBetweenReconnectAttempts ;
252
+ }
253
+
229
254
/**
230
255
* If the broker should publish a last will message in the name of the client when the client
231
256
* disconnects abruptly, this setting defines the topic on which the message will be published.
Original file line number Diff line number Diff line change @@ -409,7 +409,8 @@ protected function performConnectionHandshake(bool $useCleanSession = false): vo
409
409
*/
410
410
protected function reconnect (): void
411
411
{
412
- $ maxReconnectAttempts = $ this ->settings ->getMaxReconnectAttempts ();
412
+ $ maxReconnectAttempts = $ this ->settings ->getMaxReconnectAttempts ();
413
+ $ delayBetweenReconnectAttempts = $ this ->settings ->getDelayBetweenReconnectAttempts ();
413
414
414
415
for ($ i = 1 ; $ i <= $ maxReconnectAttempts ; $ i ++) {
415
416
try {
@@ -420,6 +421,10 @@ protected function reconnect(): void
420
421
if ($ i === $ maxReconnectAttempts ) {
421
422
throw $ e ;
422
423
}
424
+
425
+ if ($ delayBetweenReconnectAttempts > 0 ) {
426
+ usleep ($ delayBetweenReconnectAttempts * 1000 );
427
+ }
423
428
}
424
429
}
425
430
}
You can’t perform that action at this time.
0 commit comments