Skip to content

Commit

Permalink
Optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
sy-records committed Feb 23, 2021
1 parent b3c3f13 commit 99a85bf
Showing 1 changed file with 9 additions and 14 deletions.
23 changes: 9 additions & 14 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ private function reConnect()
} else {
$errMsg = socket_strerror($this->client->errCode);
}
$this->client->close();
throw new ConnectException($errMsg, $this->client->errCode);
}
$this->sleep($delay);
Expand Down Expand Up @@ -216,16 +217,14 @@ public function recv()
if ($response === '' || !$this->client->isConnected()) {
$this->reConnect();
$this->connect($this->getConnectData('clean_session') ?? true, $this->getConnectData('will') ?? []);
} elseif ($response === false) {
if ($this->client->errCode !== SOCKET_ETIMEDOUT) {
if ($this->isCoroutineClientType()) {
$errMsg = $this->client->errMsg;
} else {
$errMsg = socket_strerror($this->client->errCode);
}
$this->client->close();
throw new ConnectException($errMsg, $this->client->errCode);
} elseif ($response === false && $this->client->errCode !== SOCKET_ETIMEDOUT) {
if ($this->isCoroutineClientType()) {
$errMsg = $this->client->errMsg;
} else {
$errMsg = socket_strerror($this->client->errCode);
}
$this->client->close();
throw new ConnectException($errMsg, $this->client->errCode);
} elseif (is_string($response) && strlen($response) > 0) {
if ($this->getConfig()->getProtocolLevel() === Protocol\ProtocolInterface::MQTT_PROTOCOL_LEVEL_5_0) {
return Protocol\V5::unpack($response);
Expand Down Expand Up @@ -257,11 +256,7 @@ protected function getResponse()

protected function isCoroutineClientType(): bool
{
if ($this->clientType === self::COROUTINE_CLIENT_TYPE) {
return true;
}

return false;
return $this->clientType === self::COROUTINE_CLIENT_TYPE;
}

public function buildMessageId(): int
Expand Down

0 comments on commit 99a85bf

Please sign in to comment.