This commit is contained in:
2021-08-12 15:07:12 +08:00
parent 6ea406251c
commit 0d6176e4e2
13 changed files with 141 additions and 302 deletions
+3
View File
@@ -9,6 +9,8 @@ use Kiri\Exception\ConfigException;
use Kiri\Exception\NotFindClassException;
use Kiri\Kiri;
use ReflectionException;
use Server\Constrict\ResponseEmitter;
use Server\Constrict\TcpEmitter;
use Server\ExceptionHandlerDispatcher;
use Server\ExceptionHandlerInterface;
use Server\SInterface\OnRequest;
@@ -47,6 +49,7 @@ abstract class Http extends Server implements OnRequest
$exceptionHandler = ExceptionHandlerDispatcher::class;
}
$this->exceptionHandler = Kiri::getDi()->get($exceptionHandler);
$this->responseEmitter = Kiri::getDi()->get(ResponseEmitter::class);
}
}
+2 -3
View File
@@ -3,6 +3,7 @@
namespace Server\Abstracts;
use Annotation\Inject;
use Server\Constrict\Emitter;
use Server\Constrict\Response as CResponse;
use Server\Constrict\ResponseEmitter;
@@ -18,9 +19,7 @@ trait ResponseHelper
public CResponse $response;
/** @var ResponseEmitter */
#[Inject(ResponseEmitter::class)]
public ResponseEmitter $responseEmitter;
public Emitter $responseEmitter;
}
+3
View File
@@ -7,6 +7,8 @@ use Kiri\Exception\ConfigException;
use Kiri\Exception\NotFindClassException;
use Kiri\Kiri;
use ReflectionException;
use Server\Constrict\TcpEmitter;
use Server\Constrict\UdpEmitter;
use Server\ExceptionHandlerDispatcher;
use Server\ExceptionHandlerInterface;
use Server\SInterface\OnReceive;
@@ -37,6 +39,7 @@ abstract class Tcp extends Server implements OnReceive
$exceptionHandler = ExceptionHandlerDispatcher::class;
}
$this->exceptionHandler = Kiri::getDi()->get($exceptionHandler);
$this->responseEmitter = Kiri::getDi()->get(TcpEmitter::class);
}
}
+2
View File
@@ -7,6 +7,7 @@ use Kiri\Exception\ConfigException;
use Kiri\Exception\NotFindClassException;
use Kiri\Kiri;
use ReflectionException;
use Server\Constrict\UdpEmitter;
use Server\ExceptionHandlerDispatcher;
use Server\ExceptionHandlerInterface;
use Server\SInterface\OnPacket;
@@ -42,6 +43,7 @@ abstract class Udp extends Server implements OnPacket
$exceptionHandler = ExceptionHandlerDispatcher::class;
}
$this->exceptionHandler = Kiri::getDi()->get($exceptionHandler);
$this->responseEmitter = Kiri::getDi()->get(UdpEmitter::class);
}
}
+3
View File
@@ -8,6 +8,8 @@ use Kiri\Exception\ConfigException;
use Kiri\Exception\NotFindClassException;
use Kiri\Kiri;
use ReflectionException;
use Server\Constrict\ResponseEmitter;
use Server\Constrict\WebSocketEmitter;
use Server\ExceptionHandlerDispatcher;
use Server\ExceptionHandlerInterface;
use Server\SInterface\OnClose;
@@ -45,6 +47,7 @@ abstract class Websocket extends Server implements OnHandshake, OnMessage, OnClo
$exceptionHandler = ExceptionHandlerDispatcher::class;
}
$this->exceptionHandler = Kiri::getDi()->get($exceptionHandler);
$this->responseEmitter = Kiri::getDi()->get(WebSocketEmitter::class);
}
+18
View File
@@ -0,0 +1,18 @@
<?php
namespace Server\Constrict;
use Server\ResponseInterface;
interface Emitter
{
/**
* @param mixed $response
* @param ResponseInterface $emitter
* @return mixed
*/
public function sender(mixed $response, ResponseInterface $emitter): void;
}
-20
View File
@@ -28,10 +28,6 @@ class ResponseEmitter
public function sender(\Swoole\Http\Response|\Swoole\Http2\Response|Server $response, ResponseInterface $emitter)
{
$content = $emitter->configure($response)->getContent();
if ($response instanceof Server) {
$this->sendTcpData($response, $emitter, $content);
return;
}
if ($content instanceof FileFormatter) {
$this->download($content->getData(), $response);
} else {
@@ -40,22 +36,6 @@ class ResponseEmitter
}
}
/**
* @param Server $response
* @param ResponseInterface $emitter
* @param IFormatter $formatter
*/
private function sendTcpData(Server $response, ResponseInterface $emitter, IFormatter $formatter)
{
if ($formatter instanceof FileFormatter) {
$response->sendfile($emitter->getClientId(), $formatter->getData());
} else {
$response->send($emitter->getClientId(), $formatter->getData());
}
}
const IMAGES = [
'png' => 'image/png',
'jpeg' => 'image/jpeg',
+34
View File
@@ -0,0 +1,34 @@
<?php
namespace Server\Constrict;
use HttpServer\Http\Formatter\FileFormatter;
use Kiri\Exception\NotFindClassException;
use Server\ResponseInterface;
use Swoole\Server;
/**
*
*/
class TcpEmitter implements Emitter
{
/**
* @param Server $response
* @param ResponseInterface $emitter
* @throws NotFindClassException
* @throws \ReflectionException
* @throws \Exception
*/
public function sender(Server $response, ResponseInterface $emitter)
{
$formatter = $emitter->getContent();
if ($formatter instanceof FileFormatter) {
$response->sendfile($emitter->getClientId(), $formatter->getData());
} else {
$response->send($emitter->getClientId(), $formatter->getData());
}
}
}
+31
View File
@@ -0,0 +1,31 @@
<?php
namespace Server\Constrict;
use Kiri\Exception\NotFindClassException;
use Server\ResponseInterface;
use Swoole\Server;
/**
*
*/
class UdpEmitter implements Emitter
{
/**
* @param Server $response
* @param ResponseInterface $emitter
* @throws NotFindClassException
* @throws \ReflectionException
* @throws \Exception
*/
public function sender(Server $response, ResponseInterface $emitter)
{
$clientInfo = $emitter->getClientInfo();
$response->sendto($clientInfo['host'], $clientInfo['port'],
$emitter->getContent()->getData()
);
}
}
+28
View File
@@ -0,0 +1,28 @@
<?php
namespace Server\Constrict;
use Kiri\Exception\NotFindClassException;
use Server\ResponseInterface;
use Swoole\WebSocket\Server;
/**
*
*/
class WebSocketEmitter implements Emitter
{
/**
* @param Server $response
* @param ResponseInterface $emitter
* @throws NotFindClassException
* @throws \ReflectionException
* @throws \Exception
*/
public function sender(Server $response, ResponseInterface $emitter)
{
$response->push($emitter->getClientId(), $emitter->getContent()->getData());
}
}
-2
View File
@@ -37,8 +37,6 @@ class Http extends \Server\Abstracts\Http implements OnClose, OnConnect
/**
* @param Request $request
* @param Response $response
* @throws NotFindClassException
* @throws ReflectionException
*/
public function onRequest(Request $request, Response $response): void
{