From 2170b8825d54ac06b4cb20f34f92b1ac7fc97d7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B2=88=E5=94=81?= <52o@qq52o.cn> Date: Sat, 6 Nov 2021 10:40:41 +0800 Subject: [PATCH] Add handleVerbose (#65) --- src/Client.php | 27 +++++++++++++++++++++++++++ src/Config/ClientConfig.php | 14 ++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/src/Client.php b/src/Client.php index 658187b..83aab14 100644 --- a/src/Client.php +++ b/src/Client.php @@ -17,6 +17,7 @@ use Simps\MQTT\Exception\ConnectException; use Simps\MQTT\Exception\ProtocolException; use Simps\MQTT\Hex\ReasonCode; +use Simps\MQTT\Tools\Common; use Swoole\Coroutine; class Client @@ -224,6 +225,8 @@ public function recv() } elseif ($response === false && $this->client->errCode !== SOCKET_ETIMEDOUT) { $this->handleException(); } elseif (is_string($response) && strlen($response) > 0) { + $this->handleVerbose($response); + if ($this->getConfig()->isMQTT5()) { return Protocol\V5::unpack($response); } @@ -312,4 +315,28 @@ public function getClient() { return $this->client; } + + protected function handleVerbose(string $data) + { + switch ($this->getConfig()->getVerbose()) { + case MQTT_VERBOSE_HEXDUMP: + echo Common::hexDump($data), PHP_EOL; + break; + case MQTT_VERBOSE_HEXDUMP_ASCII: + echo Common::hexDumpAscii($data), PHP_EOL; + break; + case MQTT_VERBOSE_ASCII: + echo Common::ascii($data), PHP_EOL; + break; + case MQTT_VERBOSE_TEXT: + echo Common::printableText($data), PHP_EOL; + break; + case MQTT_VERBOSE_HEX_STREAM: + echo Common::hexStream($data), PHP_EOL; + break; + case MQTT_VERBOSE_NONE: + default: + break; + } + } } diff --git a/src/Config/ClientConfig.php b/src/Config/ClientConfig.php index 980a35e..687f783 100644 --- a/src/Config/ClientConfig.php +++ b/src/Config/ClientConfig.php @@ -41,6 +41,8 @@ class ClientConfig extends AbstractConfig protected $sockType = SWOOLE_SOCK_TCP; + protected $verbose = MQTT_VERBOSE_NONE; + public function getClientId(): string { return $this->clientId; @@ -181,4 +183,16 @@ public function isMQTT5(): bool { return $this->getProtocolLevel() === ProtocolInterface::MQTT_PROTOCOL_LEVEL_5_0; } + + public function getVerbose(): int + { + return $this->verbose; + } + + public function setVerbose(int $verbose) + { + $this->verbose = $verbose; + + return $this; + } }