From 156bc093297ff017b7de3e14d399fde56fd82ac3 Mon Sep 17 00:00:00 2001 From: Nadyita Date: Sat, 2 Mar 2024 22:44:34 +0100 Subject: [PATCH] Track last ping and package time --- src/Client/Basic.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/Client/Basic.php b/src/Client/Basic.php index b70b241..9a5ca18 100644 --- a/src/Client/Basic.php +++ b/src/Client/Basic.php @@ -28,6 +28,9 @@ class Basic { /** @var array */ private array $publicGroups = []; + private float $lastPackage = 0; + private float $lastPing = 0; + /** * True when the bot has finished receiving the initial * buddylist updates which don't correspond to state changes. @@ -63,6 +66,16 @@ public function isReady(): bool { return $this->isReady; } + /** Get the UNIX timestamp when the last package was received */ + public function getLastPackageReceived(): float { + return $this->lastPackage; + } + + /** Get the UNIX timestamp when the last ping package was received */ + public function getLastPingReceived(): float { + return $this->lastPing; + } + /** * Request a callback when the connection is ready to * process packages and has finished receiving the @@ -196,7 +209,11 @@ public function read(): ?Package\In { $this->logger?->info("Stream has closed the connection"); return null; } + $this->lastPackage = microtime(true); $package = $this->parser->parseBinaryPackage($binPackage); + if ($package instanceof Package\In\Ping) { + $this->lastPing = microtime(true); + } $this->handleIncomingPackage($package); return $package; }