Skip to content

Commit

Permalink
Add handleVerbose (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
sy-records authored Nov 6, 2021
1 parent 25910df commit 2170b88
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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;
}
}
}
14 changes: 14 additions & 0 deletions src/Config/ClientConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}

0 comments on commit 2170b88

Please sign in to comment.