From 0df04711b54e41d656cb612c83b876d20b33ce5c Mon Sep 17 00:00:00 2001 From: mmasiukevich Date: Sat, 4 Dec 2021 12:47:40 +0100 Subject: [PATCH] Merge fix --- src/Connection.php | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/src/Connection.php b/src/Connection.php index 3c8ff1a..ff1562f 100644 --- a/src/Connection.php +++ b/src/Connection.php @@ -54,7 +54,7 @@ final class Connection private $lastRead = 0; /** - * @var string + * @var string|null */ private $heartbeatWatcherId; @@ -168,35 +168,38 @@ public function heartbeat(int $interval, ?callable $connectionLost = null): void { $this->heartbeatWatcherId = Loop::repeat( $interval, - function ($watcherId) use ($interval, $connectionLost){ - $currentTime = Loop::now(); + function (string $watcherId) use ($interval, $connectionLost){ + $currentTime = Loop::now(); - if (null !== $this->socket) { - $lastWrite = $this->lastWrite ?: $currentTime; + if (null !== $this->socket) { + $lastWrite = $this->lastWrite ?: $currentTime; - $nextHeartbeat = $lastWrite + $interval; + $nextHeartbeat = $lastWrite + $interval; - if ($currentTime >= $nextHeartbeat) { - yield $this->write((new Buffer) - ->appendUint8(8) - ->appendUint16(0) - ->appendUint32(0) - ->appendUint8(206) - ); - } + if ($currentTime >= $nextHeartbeat) { + yield $this->write((new Buffer) + ->appendUint8(8) + ->appendUint16(0) + ->appendUint32(0) + ->appendUint8(206) + ); + } - unset($lastWrite, $nextHeartbeat); - } + unset($lastWrite, $nextHeartbeat); + } - if (null !== $connectionLost && 0 !== $this->lastRead) { - if ($currentTime > ($this->lastRead + $interval + 1000)) { + if ( + null !== $connectionLost && + 0 !== $this->lastRead && + $currentTime > ($this->lastRead + $interval + 1000) + ) + { $connectionLost(); Loop::cancel($watcherId); } - } - unset($currentTime); - }); + unset($currentTime); + }); } public function close(): void