From 1bb8906a73f8f5304253bf465bf6d7c96518c0d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Mon, 29 Mar 2021 18:41:51 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Rpc/Client.php | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/Rpc/Client.php b/Rpc/Client.php index f1486064..deb29bea 100644 --- a/Rpc/Client.php +++ b/Rpc/Client.php @@ -5,9 +5,12 @@ namespace Rpc; use Exception; +use ReflectionException; use Snowflake\Abstracts\Component; use Snowflake\Channel; use Snowflake\Core\Json; +use Snowflake\Exception\ComponentException; +use Snowflake\Exception\NotFindClassException; use Snowflake\Snowflake; use Swoole\Coroutine\Client as CClient; @@ -42,10 +45,11 @@ class Client extends Component if (!($this->client instanceof CClient)) { $this->client = $this->getClient(); } - if (!$this->client->isConnected() || !$this->connect()) { + if (!$this->client->isConnected() && !$this->connect()) { return false; } $isSend = $this->client->send(Json::encode(['cmd' => $cmd, 'body' => $param])); + $this->recover(); if ($isSend === false) { return $this->addError($this->client->errMsg . '(' . $this->client->errCode . ')'); } @@ -56,6 +60,22 @@ class Client extends Component } + /** + * @throws ReflectionException + * @throws ComponentException + * @throws NotFindClassException + * @throws Exception + */ + public function recover(): static + { + /** @var Channel $channel */ + $channel = Snowflake::app()->get('channel'); + $channel->push(CClient::class, $this->client); + $this->client = null; + return $this; + } + + /** * @return bool * @throws Exception