From fda5449b0213c05b11ce4ba8aaa161dadc17b749 Mon Sep 17 00:00:00 2001 From: jonathan Date: Sun, 30 Dec 2018 23:07:44 +0000 Subject: [PATCH 1/2] Cancel keep alive if client gets disconnected from server --- src/MqttClient.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/MqttClient.php b/src/MqttClient.php index 58e1683..f70975b 100644 --- a/src/MqttClient.php +++ b/src/MqttClient.php @@ -107,10 +107,14 @@ private function keepAlive(Connection $stream, $keepAlive) if($keepAlive > 0) { $interval = $keepAlive / 2; - $this->getLoop()->addPeriodicTimer($interval, function(Timer $timer) use ($stream) { + $timer = $this->getLoop()->addPeriodicTimer($interval, function(Timer $timer) use ($stream) { $packet = new PingRequest($this->version); $this->sendPacketToStream($stream, $packet); }); + + $stream->on('close', function() use ($timer) { + $this->getLoop()->cancelTimer($timer); + }); } return new FulfilledPromise($stream); From c1a2a45c3f4bf88204844076e62596c51010669a Mon Sep 17 00:00:00 2001 From: jonathan Date: Sun, 30 Dec 2018 23:10:01 +0000 Subject: [PATCH 2/2] Remove unused Timer reference --- src/MqttClient.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/MqttClient.php b/src/MqttClient.php index f70975b..d834b99 100644 --- a/src/MqttClient.php +++ b/src/MqttClient.php @@ -23,7 +23,6 @@ use oliverlorenz\reactphpmqtt\protocol\Version; use oliverlorenz\reactphpmqtt\protocol\Violation as ProtocolViolation; use React\EventLoop\LoopInterface as Loop; -use React\EventLoop\Timer\Timer; use React\Promise\Deferred; use React\Promise\FulfilledPromise; use React\Promise\PromiseInterface; @@ -107,7 +106,7 @@ private function keepAlive(Connection $stream, $keepAlive) if($keepAlive > 0) { $interval = $keepAlive / 2; - $timer = $this->getLoop()->addPeriodicTimer($interval, function(Timer $timer) use ($stream) { + $timer = $this->getLoop()->addPeriodicTimer($interval, function() use ($stream) { $packet = new PingRequest($this->version); $this->sendPacketToStream($stream, $packet); });