This commit is contained in:
2020-11-01 05:10:38 +08:00
parent a24e431052
commit bf1cc479b9
+19 -27
View File
@@ -5,6 +5,7 @@ namespace HttpServer\Client;
use Exception; use Exception;
use HttpServer\Http\Context;
use Snowflake\Abstracts\Component; use Snowflake\Abstracts\Component;
use Snowflake\Core\Help; use Snowflake\Core\Help;
use Snowflake\Core\JSON; use Snowflake\Core\JSON;
@@ -19,14 +20,6 @@ use \Swoole\Coroutine\Http2\Client as H2Client;
class Http2 extends Component class Http2 extends Component
{ {
/** @var H2Client[] */
private array $_clients = [];
/** @var Request[] */
private array $_requests = [];
/** /**
* @param $domain * @param $domain
* @param $path * @param $path
@@ -59,7 +52,6 @@ class Http2 extends Component
} }
/** /**
* @param $domain * @param $domain
* @param $path * @param $path
@@ -76,7 +68,6 @@ class Http2 extends Component
} }
/** /**
* @param $domain * @param $domain
* @param $path * @param $path
@@ -103,22 +94,22 @@ class Http2 extends Component
*/ */
public function getRequest($domain, $path, $method, $params) public function getRequest($domain, $path, $method, $params)
{ {
if (isset($this->_requests[$domain . $path])) { if (Context::hasContext($domain . $path)) {
$req = $this->_requests[$domain . $path]; $req = Context::getContext($domain . $path);
} else { } else {
$req = new Request(); $req = new Request();
$this->_requests[$domain . $path] = $req; $req->method = $method;
} $req->path = $path;
$req->method = $method; $req->headers = [
$req->path = $path; 'host' => $domain,
$req->headers = [ 'user-agent' => 'Chrome/49.0.2587.3',
'host' => $domain, 'accept' => 'text/html,application/json',
'user-agent' => 'Chrome/49.0.2587.3', 'accept-encoding' => 'gzip'
'accept' => 'text/html,application/json', ];
'accept-encoding' => 'gzip' if (!is_string($params)) {
]; $params = JSON::encode($params);
if (!is_string($params)) { }
$params = JSON::encode($params); Context::setContext($domain . $path, $req);
} }
$req->data = $params; $req->data = $params;
return $req; return $req;
@@ -134,8 +125,8 @@ class Http2 extends Component
*/ */
private function getClient($domain, $path, $timeout = -1) private function getClient($domain, $path, $timeout = -1)
{ {
if (isset($this->_clients[$path])) { if (Context::hasContext($domain)) {
return $this->_clients[$path]; return Context::getContext($domain);
} }
$client = new H2Client($domain, 443, true); $client = new H2Client($domain, 443, true);
$client->set([ $client->set([
@@ -145,7 +136,8 @@ class Http2 extends Component
if (!$client->connect()) { if (!$client->connect()) {
throw new Exception('Connected fail.'); throw new Exception('Connected fail.');
} }
return $this->_clients[$domain . $path] = $client;
return Context::setContext($domain, $client);
} }