From bf1cc479b9d9e1c8c731e5ce18ecb14d5bb752b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Sun, 1 Nov 2020 05:10:38 +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 --- HttpServer/Client/Http2.php | 46 +++++++++++++++---------------------- 1 file changed, 19 insertions(+), 27 deletions(-) diff --git a/HttpServer/Client/Http2.php b/HttpServer/Client/Http2.php index 2f018b32..114afd94 100644 --- a/HttpServer/Client/Http2.php +++ b/HttpServer/Client/Http2.php @@ -5,6 +5,7 @@ namespace HttpServer\Client; use Exception; +use HttpServer\Http\Context; use Snowflake\Abstracts\Component; use Snowflake\Core\Help; use Snowflake\Core\JSON; @@ -19,14 +20,6 @@ use \Swoole\Coroutine\Http2\Client as H2Client; class Http2 extends Component { - /** @var H2Client[] */ - private array $_clients = []; - - - /** @var Request[] */ - private array $_requests = []; - - /** * @param $domain * @param $path @@ -59,7 +52,6 @@ class Http2 extends Component } - /** * @param $domain * @param $path @@ -76,7 +68,6 @@ class Http2 extends Component } - /** * @param $domain * @param $path @@ -103,22 +94,22 @@ class Http2 extends Component */ public function getRequest($domain, $path, $method, $params) { - if (isset($this->_requests[$domain . $path])) { - $req = $this->_requests[$domain . $path]; + if (Context::hasContext($domain . $path)) { + $req = Context::getContext($domain . $path); } else { $req = new Request(); - $this->_requests[$domain . $path] = $req; - } - $req->method = $method; - $req->path = $path; - $req->headers = [ - 'host' => $domain, - 'user-agent' => 'Chrome/49.0.2587.3', - 'accept' => 'text/html,application/json', - 'accept-encoding' => 'gzip' - ]; - if (!is_string($params)) { - $params = JSON::encode($params); + $req->method = $method; + $req->path = $path; + $req->headers = [ + 'host' => $domain, + 'user-agent' => 'Chrome/49.0.2587.3', + 'accept' => 'text/html,application/json', + 'accept-encoding' => 'gzip' + ]; + if (!is_string($params)) { + $params = JSON::encode($params); + } + Context::setContext($domain . $path, $req); } $req->data = $params; return $req; @@ -134,8 +125,8 @@ class Http2 extends Component */ private function getClient($domain, $path, $timeout = -1) { - if (isset($this->_clients[$path])) { - return $this->_clients[$path]; + if (Context::hasContext($domain)) { + return Context::getContext($domain); } $client = new H2Client($domain, 443, true); $client->set([ @@ -145,7 +136,8 @@ class Http2 extends Component if (!$client->connect()) { throw new Exception('Connected fail.'); } - return $this->_clients[$domain . $path] = $client; + + return Context::setContext($domain, $client); }