111
This commit is contained in:
@@ -1,50 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Abstracts;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Http\Handler\Router;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use Server\Constrict\ResponseEmitter;
|
||||
use Server\ExceptionHandlerDispatcher;
|
||||
use Server\ExceptionHandlerInterface;
|
||||
use Server\SInterface\OnRequest;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
abstract class Http extends Server implements OnRequest
|
||||
{
|
||||
|
||||
|
||||
use EventDispatchHelper;
|
||||
use ResponseHelper;
|
||||
|
||||
/** @var Router|mixed */
|
||||
#[Inject(Router::class)]
|
||||
public Router $router;
|
||||
|
||||
|
||||
/**
|
||||
* @var ExceptionHandlerInterface
|
||||
*/
|
||||
public ExceptionHandlerInterface $exceptionHandler;
|
||||
|
||||
|
||||
/**
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$exceptionHandler = Config::get('exception.http', ExceptionHandlerDispatcher::class);
|
||||
if (!in_array(ExceptionHandlerInterface::class, class_implements($exceptionHandler))) {
|
||||
$exceptionHandler = ExceptionHandlerDispatcher::class;
|
||||
}
|
||||
$this->exceptionHandler = Kiri::getDi()->get($exceptionHandler);
|
||||
$this->responseEmitter = Kiri::getDi()->get(ResponseEmitter::class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Abstracts;
|
||||
|
||||
use Kiri\Abstracts\Config;
|
||||
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;
|
||||
|
||||
abstract class Tcp extends Server implements OnReceive
|
||||
{
|
||||
|
||||
use EventDispatchHelper;
|
||||
use ResponseHelper;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @var ExceptionHandlerInterface
|
||||
*/
|
||||
public ExceptionHandlerInterface $exceptionHandler;
|
||||
|
||||
|
||||
/**
|
||||
* @throws ReflectionException
|
||||
* @throws ConfigException
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$exceptionHandler = Config::get('exception.tcp', ExceptionHandlerDispatcher::class);
|
||||
if (!in_array(ExceptionHandlerInterface::class, class_implements($exceptionHandler))) {
|
||||
$exceptionHandler = ExceptionHandlerDispatcher::class;
|
||||
}
|
||||
$this->exceptionHandler = Kiri::getDi()->get($exceptionHandler);
|
||||
$this->responseEmitter = Kiri::getDi()->get(TcpEmitter::class);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,49 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Abstracts;
|
||||
|
||||
use Kiri\Abstracts\Config;
|
||||
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;
|
||||
use Server\SInterface\OnReceive;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
abstract class Udp extends Server implements OnPacket
|
||||
{
|
||||
|
||||
use EventDispatchHelper;
|
||||
use ResponseHelper;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @var ExceptionHandlerInterface
|
||||
*/
|
||||
public ExceptionHandlerInterface $exceptionHandler;
|
||||
|
||||
|
||||
/**
|
||||
* @throws ReflectionException
|
||||
* @throws ConfigException
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$exceptionHandler = Config::get('exception.udp', ExceptionHandlerDispatcher::class);
|
||||
if (!in_array(ExceptionHandlerInterface::class, class_implements($exceptionHandler))) {
|
||||
$exceptionHandler = ExceptionHandlerDispatcher::class;
|
||||
}
|
||||
$this->exceptionHandler = Kiri::getDi()->get($exceptionHandler);
|
||||
$this->responseEmitter = Kiri::getDi()->get(UdpEmitter::class);
|
||||
}
|
||||
|
||||
}
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Abstracts;
|
||||
namespace Server\Abstracts\Utility;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Kiri\Events\EventDispatch;
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Abstracts;
|
||||
namespace Server\Abstracts\Utility;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Server\Constrict\Emitter;
|
||||
@@ -1,82 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Abstracts;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use ReflectionException;
|
||||
use Server\Constrict\WebSocketEmitter;
|
||||
use Server\ExceptionHandlerDispatcher;
|
||||
use Server\ExceptionHandlerInterface;
|
||||
use Server\SInterface\OnClose;
|
||||
use Server\SInterface\OnHandshake;
|
||||
use Server\SInterface\OnMessage;
|
||||
use Swoole\Http\Request;
|
||||
use Swoole\Http\Response;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
abstract class Websocket extends Server implements OnHandshake, OnMessage, OnClose
|
||||
{
|
||||
|
||||
use EventDispatchHelper;
|
||||
use ResponseHelper;
|
||||
|
||||
|
||||
/**
|
||||
* @var ExceptionHandlerInterface
|
||||
*/
|
||||
public ExceptionHandlerInterface $exceptionHandler;
|
||||
|
||||
|
||||
/**
|
||||
* @throws ReflectionException
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$exceptionHandler = Config::get('exception.websocket', ExceptionHandlerDispatcher::class);
|
||||
if (!in_array(ExceptionHandlerInterface::class, class_implements($exceptionHandler))) {
|
||||
$exceptionHandler = ExceptionHandlerDispatcher::class;
|
||||
}
|
||||
$this->exceptionHandler = Kiri::getDi()->get($exceptionHandler);
|
||||
$this->responseEmitter = Kiri::getDi()->get(WebSocketEmitter::class);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandshake(Request $request, Response $response): void
|
||||
{
|
||||
// TODO: Implement OnHandshake() method.
|
||||
$secWebSocketKey = $request->header['sec-websocket-key'];
|
||||
$patten = '#^[+/0-9A-Za-z]{21}[AQgw]==$#';
|
||||
if (0 === preg_match($patten, $secWebSocketKey) || 16 !== strlen(base64_decode($secWebSocketKey))) {
|
||||
throw new Exception('protocol error.', 500);
|
||||
}
|
||||
$key = base64_encode(sha1($request->header['sec-websocket-key'] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11', TRUE));
|
||||
$headers = [
|
||||
'Upgrade' => 'websocket',
|
||||
'Connection' => 'Upgrade',
|
||||
'Sec-websocket-Accept' => $key,
|
||||
'Sec-websocket-Version' => '13',
|
||||
];
|
||||
if (isset($request->header['sec-websocket-protocol'])) {
|
||||
$explode = explode(',',$request->header['sec-websocket-protocol']);
|
||||
$headers['Sec-websocket-Protocol'] = $explode[0];
|
||||
}
|
||||
foreach ($headers as $key => $val) {
|
||||
$response->setHeader($key, $val);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -9,17 +9,26 @@ use Http\Handler\Context;
|
||||
use Http\Handler\Abstracts\HandlerManager;
|
||||
use Http\Handler\Dispatcher;
|
||||
use Http\Handler\Handler;
|
||||
use Http\Handler\Router;
|
||||
use Http\Message\ContentType;
|
||||
use Http\Message\ServerRequest;
|
||||
use Http\Message\Stream;
|
||||
use Http\Handler\Abstracts\MiddlewareManager;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use Server\Abstracts\Utility\EventDispatchHelper;
|
||||
use Server\Abstracts\Utility\ResponseHelper;
|
||||
use Server\Constrict\RequestInterface;
|
||||
use Server\Constrict\ResponseEmitter;
|
||||
use Server\Constrict\ResponseInterface;
|
||||
use Server\Events\OnAfterRequest;
|
||||
use Server\ExceptionHandlerDispatcher;
|
||||
use Server\ExceptionHandlerInterface;
|
||||
use Server\SInterface\OnClose;
|
||||
use Server\SInterface\OnConnect;
|
||||
use Server\SInterface\OnRequest;
|
||||
use Swoole\Http\Request;
|
||||
use Swoole\Http\Response;
|
||||
use Swoole\Server;
|
||||
@@ -27,9 +36,36 @@ use Swoole\Server;
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class Http extends \Server\Abstracts\Http implements OnClose, OnConnect
|
||||
class Http implements OnClose, OnConnect, OnRequest
|
||||
{
|
||||
|
||||
use EventDispatchHelper;
|
||||
use ResponseHelper;
|
||||
|
||||
/** @var Router|mixed */
|
||||
#[Inject(Router::class)]
|
||||
public Router $router;
|
||||
|
||||
|
||||
/**
|
||||
* @var ExceptionHandlerInterface
|
||||
*/
|
||||
public ExceptionHandlerInterface $exceptionHandler;
|
||||
|
||||
|
||||
/**
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$exceptionHandler = Config::get('exception.http', ExceptionHandlerDispatcher::class);
|
||||
if (!in_array(ExceptionHandlerInterface::class, class_implements($exceptionHandler))) {
|
||||
$exceptionHandler = ExceptionHandlerDispatcher::class;
|
||||
}
|
||||
$this->exceptionHandler = Kiri::getDi()->get($exceptionHandler);
|
||||
$this->responseEmitter = Kiri::getDi()->get(ResponseEmitter::class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
|
||||
@@ -2,19 +2,52 @@
|
||||
|
||||
namespace Server\Service;
|
||||
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use Server\Abstracts\Utility\EventDispatchHelper;
|
||||
use Server\Abstracts\Utility\ResponseHelper;
|
||||
use Server\Constrict\TcpEmitter;
|
||||
use Server\ExceptionHandlerDispatcher;
|
||||
use Server\ExceptionHandlerInterface;
|
||||
use Server\SInterface\OnClose;
|
||||
use Server\SInterface\OnConnect;
|
||||
use Server\SInterface\OnReceive;
|
||||
use Swoole\Server;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class Tcp extends \Server\Abstracts\Tcp implements OnConnect, OnClose
|
||||
class Tcp implements OnConnect, OnClose, OnReceive
|
||||
{
|
||||
|
||||
use EventDispatchHelper;
|
||||
use ResponseHelper;
|
||||
|
||||
/**
|
||||
|
||||
/**
|
||||
* @var ExceptionHandlerInterface
|
||||
*/
|
||||
public ExceptionHandlerInterface $exceptionHandler;
|
||||
|
||||
|
||||
/**
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$exceptionHandler = Config::get('exception.tcp', ExceptionHandlerDispatcher::class);
|
||||
if (!in_array(ExceptionHandlerInterface::class, class_implements($exceptionHandler))) {
|
||||
$exceptionHandler = ExceptionHandlerDispatcher::class;
|
||||
}
|
||||
$this->exceptionHandler = Kiri::getDi()->get($exceptionHandler);
|
||||
$this->responseEmitter = Kiri::getDi()->get(TcpEmitter::class);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
*/
|
||||
|
||||
@@ -4,17 +4,51 @@ namespace Server\Service;
|
||||
|
||||
|
||||
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use Server\Abstracts\Server;
|
||||
use Server\Abstracts\Utility\EventDispatchHelper;
|
||||
use Server\Abstracts\Utility\ResponseHelper;
|
||||
use Server\Constrict\UdpEmitter;
|
||||
use Server\ExceptionHandlerDispatcher;
|
||||
use Server\ExceptionHandlerInterface;
|
||||
use Server\SInterface\OnPacket;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class Udp extends \Server\Abstracts\Udp
|
||||
class Udp implements OnPacket
|
||||
{
|
||||
|
||||
use EventDispatchHelper;
|
||||
use ResponseHelper;
|
||||
|
||||
/**
|
||||
|
||||
|
||||
/**
|
||||
* @var ExceptionHandlerInterface
|
||||
*/
|
||||
public ExceptionHandlerInterface $exceptionHandler;
|
||||
|
||||
|
||||
/**
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$exceptionHandler = Config::get('exception.udp', ExceptionHandlerDispatcher::class);
|
||||
if (!in_array(ExceptionHandlerInterface::class, class_implements($exceptionHandler))) {
|
||||
$exceptionHandler = ExceptionHandlerDispatcher::class;
|
||||
}
|
||||
$this->exceptionHandler = Kiri::getDi()->get($exceptionHandler);
|
||||
$this->responseEmitter = Kiri::getDi()->get(UdpEmitter::class);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param string $data
|
||||
* @param array $clientInfo
|
||||
|
||||
@@ -4,6 +4,17 @@ namespace Server\Service;
|
||||
|
||||
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
use Server\Abstracts\Utility\EventDispatchHelper;
|
||||
use Server\Abstracts\Utility\ResponseHelper;
|
||||
use Server\Constrict\WebSocketEmitter;
|
||||
use Server\ExceptionHandlerDispatcher;
|
||||
use Server\ExceptionHandlerInterface;
|
||||
use Server\SInterface\OnClose;
|
||||
use Server\SInterface\OnHandshake;
|
||||
use Server\SInterface\OnMessage;
|
||||
use Server\SInterface\OnRequest;
|
||||
use Swoole\Http\Request;
|
||||
use Swoole\Http\Response;
|
||||
@@ -13,19 +24,62 @@ use Swoole\WebSocket\Frame;
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class WebSocket extends \Server\Abstracts\Websocket
|
||||
class WebSocket implements OnHandshake, OnMessage, OnClose
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
use EventDispatchHelper;
|
||||
use ResponseHelper;
|
||||
|
||||
|
||||
/**
|
||||
* @var ExceptionHandlerInterface
|
||||
*/
|
||||
public ExceptionHandlerInterface $exceptionHandler;
|
||||
|
||||
|
||||
/**
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$exceptionHandler = Config::get('exception.websocket', ExceptionHandlerDispatcher::class);
|
||||
if (!in_array(ExceptionHandlerInterface::class, class_implements($exceptionHandler))) {
|
||||
$exceptionHandler = ExceptionHandlerDispatcher::class;
|
||||
}
|
||||
$this->exceptionHandler = Kiri::getDi()->get($exceptionHandler);
|
||||
$this->responseEmitter = Kiri::getDi()->get(WebSocketEmitter::class);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandshake(Request $request, Response $response): void
|
||||
{
|
||||
parent::onHandshake($request, $response); // TODO: Change the autogenerated stub
|
||||
|
||||
// TODO: Implement OnHandshake() method.
|
||||
$secWebSocketKey = $request->header['sec-websocket-key'];
|
||||
$patten = '#^[+/0-9A-Za-z]{21}[AQgw]==$#';
|
||||
if (0 === preg_match($patten, $secWebSocketKey) || 16 !== strlen(base64_decode($secWebSocketKey))) {
|
||||
throw new Exception('protocol error.', 500);
|
||||
}
|
||||
$key = base64_encode(sha1($request->header['sec-websocket-key'] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11', TRUE));
|
||||
$headers = [
|
||||
'Upgrade' => 'websocket',
|
||||
'Connection' => 'Upgrade',
|
||||
'Sec-websocket-Accept' => $key,
|
||||
'Sec-websocket-Version' => '13',
|
||||
];
|
||||
if (isset($request->header['sec-websocket-protocol'])) {
|
||||
$explode = explode(',',$request->header['sec-websocket-protocol']);
|
||||
$headers['Sec-websocket-Protocol'] = $explode[0];
|
||||
}
|
||||
foreach ($headers as $key => $val) {
|
||||
$response->setHeader($key, $val);
|
||||
}
|
||||
$response->status(101);
|
||||
$response->end();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user