This commit is contained in:
2020-09-11 19:37:32 +08:00
parent 4d8eb95874
commit afc2eb2e0e
3 changed files with 139 additions and 3 deletions
+14
View File
@@ -11,6 +11,8 @@ namespace HttpServer\Client;
use Snowflake\Core\Help;
use Exception;
use Snowflake\Exception\ComponentException;
use Snowflake\Snowflake;
use Swoole\Coroutine;
use Swoole\Coroutine\Http\Client as SClient;
use Swoole\Coroutine\System;
@@ -18,6 +20,7 @@ use Swoole\Coroutine\System;
/**
* Class Client
* @package Snowflake\Snowflake\Http
* @property Http2 $http2
*/
class Client
{
@@ -110,6 +113,17 @@ class Client
{
}
/**
* @return Http2
* @throws ComponentException
*/
public function getHttp2()
{
return Snowflake::app()->get('http2');
}
/**
* @param $data
*/
+117
View File
@@ -0,0 +1,117 @@
<?php
namespace HttpServer\Client;
use Exception;
use Snowflake\Abstracts\Component;
use Snowflake\Core\JSON;
use Swoole\Http2\Request;
use \Swoole\Coroutine\Http2\Client as H2Client;
/**
* Class Http2
* @package HttpServer\Client
*/
class Http2 extends Component
{
/** @var H2Client[] */
private $_clients = [];
/** @var Request[] */
private $_requests = [];
/**
* @param $domain
* @param $path
* @param array $params
* @param int $timeout
* @return mixed
* @throws Exception
*/
public function query($domain, $path, $params = [], $timeout = -1)
{
$client = $this->getClient($domain, $path, $timeout);
$client->send($this->getRequest($domain, $path, 'GET', $params));
return $client->recv();
}
/**
* @param $domain
* @param $path
* @param array $params
* @param int $timeout
* @return mixed
* @throws Exception
*/
public function push($domain, $path, $params = [], $timeout = -1)
{
$client = $this->getClient($domain, $path, $timeout);
$client->send($this->getRequest($domain, $path, 'POST', $params));
return $client->recv();
}
/**
* @param $domain
* @param $path
* @param $method
* @param $params
* @return Request
* @throws Exception
*/
public function getRequest($domain, $path, $method, $params)
{
if (isset($this->_requests[$domain . $path])) {
$req = $this->_requests[$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->data = $params;
return $req;
}
/**
* @param $domain
* @param $path
* @param int $timeout
* @return H2Client
* @throws Exception
*/
private function getClient($domain, $path, $timeout = -1)
{
if (isset($this->_clients[$path])) {
return $this->_clients[$path];
}
$client = new H2Client($domain, 443, true);
$client->set([
'timeout' => $timeout,
'ssl_host_name' => $domain
]);
if (!$client->connect()) {
throw new Exception('Connected fail.');
}
return $this->_clients[$domain . $path] = $client;
}
}
+8 -3
View File
@@ -10,6 +10,8 @@ namespace Snowflake\Abstracts;
use Exception;
use HttpServer\Client\Client;
use HttpServer\Client\Http2;
use HttpServer\Http\Request;
use HttpServer\Http\Response;
use HttpServer\Route\Router;
@@ -46,6 +48,7 @@ use Database\DatabasesProviders;
* @property Logger $logger
* @property Jwt $jwt
* @property BaseGoto $goto
* @property Client $client
*/
abstract class BaseApplication extends Service
{
@@ -104,7 +107,7 @@ abstract class BaseApplication extends Service
*
* @return array
*/
protected function readLinesFromFile($filePath)
protected function readLinesFromFile(string $filePath)
{
// Read file into an array of lines with auto-detected line endings
$autodetect = ini_get('auto_detect_line_endings');
@@ -122,7 +125,7 @@ abstract class BaseApplication extends Service
*
* @return bool
*/
protected function isComment($line)
protected function isComment(string $line)
{
$line = ltrim($line);
@@ -136,7 +139,7 @@ abstract class BaseApplication extends Service
*
* @return bool
*/
protected function looksLikeSetter($line)
protected function looksLikeSetter(string $line)
{
return strpos($line, '=') !== false;
}
@@ -363,6 +366,8 @@ abstract class BaseApplication extends Service
'error' => ['class' => ErrorHandler::class],
'event' => ['class' => Event::class],
'annotation' => ['class' => Annotation::class],
'client' => ['class' => Client::class],
'http2' => ['class' => Http2::class],
'connections' => ['class' => Connection::class],
'redis_connections' => ['class' => SRedis::class],
'pool' => ['class' => SPool::class],