Skip to content

Commit e4b5f47

Browse files
bgrecoBrad GrecoNamoshek
authored
Reset the last ping time only after a ping is sent, rather than after… (#5)
* Reset the last ping time only after a ping is sent, rather than after any broker command is received. If broker messages are received too frequently, it causes pings to be never sent and the broker disconnecting the client. * Reset ping timer when data is sent to the broker Co-authored-by: Brad Greco <[email protected]> Co-authored-by: Namoshek <[email protected]>
1 parent 359c5e2 commit e4b5f47

File tree

1 file changed

+4
-3
lines changed

1 file changed

+4
-3
lines changed

src/MQTTClient.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,6 @@ protected function performConnectionHandshake(string $username = null, string $p
244244
switch ($acknowledgement[3]) {
245245
case chr(0):
246246
$this->logger->info(sprintf('Connection with MQTT broker at [%s:%s] established successfully.', $this->host, $this->port));
247-
$this->lastPingAt = microtime(true);
248247
break;
249248
case chr(1):
250249
$this->logger->error(sprintf('The MQTT broker at [%s:%s] does not support MQTT v3.', $this->host, $this->port));
@@ -653,8 +652,6 @@ public function loop(bool $allowSleep = true, bool $exitWhenQueuesEmpty = false,
653652
$this->logger->debug(sprintf('Received message with unsupported command [%s]. Skipping.', $command));
654653
break;
655654
}
656-
657-
$this->lastPingAt = microtime(true);
658655
} else {
659656
$this->logger->error('A reserved command has been received from an MQTT broker. Supported are commands (including) 1-14.', [
660657
'broker' => sprintf('%s:%s', $this->host, $this->port),
@@ -1304,6 +1301,10 @@ protected function writeToSocket(string $data, int $length = null): void
13041301
]);
13051302
throw new DataTransferException(self::EXCEPTION_TX_DATA, 'Sending data over the socket failed. Has it been closed?');
13061303
}
1304+
1305+
// After writing successfully to the socket, the broker should have received a new message from us.
1306+
// Because we only need to send a ping if no other messages are delivered, we can safely reset the ping timer.
1307+
$this->lastPingAt = microtime(true);
13071308
}
13081309

13091310
/**

0 commit comments

Comments
 (0)