From c3a89d4ddd55849d6f37d9eb95850bd91ab509b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Tue, 23 Mar 2021 18:23:45 +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 | 5 +---- Rpc/Service.php | 10 +++++++--- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/Rpc/Client.php b/Rpc/Client.php index afc3182e..202f2b8d 100644 --- a/Rpc/Client.php +++ b/Rpc/Client.php @@ -53,10 +53,7 @@ class Client extends Component return $this->addError($this->client->errMsg . '(' . $this->client->errCode . ')'); } - $response = $this->client->recv(); - $this->client->close(); - - if (is_bool($unpack = Json::decode($response))) { + if (is_bool($unpack = Json::decode($this->client->recv()))) { return $this->addError('Service return data format error(500)'); } return $unpack; diff --git a/Rpc/Service.php b/Rpc/Service.php index 7e429abf..d479d6c7 100644 --- a/Rpc/Service.php +++ b/Rpc/Service.php @@ -70,10 +70,14 @@ class Service extends Component try { /** @var Request $request */ $request = Context::getContext('request'); - if (($node = router()->find_path($this->replace($request, $service))) === null) { + if (($node = router()->find_path(Service::replace($request, $service))) === null) { throw new Exception('Cmd not find.'); } - return Json::encode($node->dispatch()); + $response = $node->dispatch(); + if (is_string($response)) { + return $response; + } + return Json::encode($response); } catch (\Throwable $exception) { $this->addError($exception); return Json::encode(['state' => 'fail', 'message' => $exception->getMessage()]); @@ -88,7 +92,7 @@ class Service extends Component * @return Request * @throws Exception */ - public function replace(Request $request, array $service): Request + public static function replace(Request $request, array $service): Request { $body = $request->params->getBodyAndClear(); if (is_null($serialize = unserialize($body))) {