改名
This commit is contained in:
@@ -5,23 +5,19 @@ namespace Server\Abstracts;
|
||||
use Annotation\Inject;
|
||||
use HttpServer\Route\Router;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Events\EventDispatch;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Exception\NotFindClassException;
|
||||
use Kiri\Kiri;
|
||||
use ReflectionException;
|
||||
use Server\Constrict\Response as CResponse;
|
||||
use Server\Constrict\ResponseEmitter;
|
||||
use Server\ExceptionHandlerDispatcher;
|
||||
use Server\ExceptionHandlerInterface;
|
||||
use Server\ListenerHelper;
|
||||
use Server\SInterface\OnRequest;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
abstract class Http implements OnRequest
|
||||
abstract class Http extends Server implements OnRequest
|
||||
{
|
||||
|
||||
|
||||
|
||||
@@ -21,93 +21,12 @@ abstract class Server
|
||||
{
|
||||
|
||||
|
||||
protected array $_events = [];
|
||||
|
||||
|
||||
protected Event $_event;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param $prefix
|
||||
* @throws ConfigException
|
||||
*/
|
||||
protected function setProcessName($prefix)
|
||||
{
|
||||
if (Kiri::getPlatform()->isMac()) {
|
||||
return;
|
||||
}
|
||||
$name = Config::get('id', 'system-service');
|
||||
if (!empty($prefix)) {
|
||||
$name .= '.' . $prefix;
|
||||
}
|
||||
swoole_set_process_name($name);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Server constructor.
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct()
|
||||
public function __construct(protected \Swoole\Server|Port $server)
|
||||
{
|
||||
$this->_event = Kiri::getApp('event');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array|null $events
|
||||
* @throws Exception
|
||||
*/
|
||||
public function setEvents(string $name, ?array $events): void
|
||||
{
|
||||
if (is_array($events) && is_string($events[0])) {
|
||||
$events[0] = Kiri::getDi()->get($events[0]);
|
||||
}
|
||||
if (!is_callable($events)) {
|
||||
return;
|
||||
}
|
||||
$this->_events[$name] = $events;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function getEvents(): array
|
||||
{
|
||||
return $this->_events;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function getEvent(string $name): mixed
|
||||
{
|
||||
return $this->_events[$name] ?? null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param Closure|null $closure
|
||||
* @param array $params
|
||||
* @return mixed
|
||||
*/
|
||||
public function runEvent($name, ?Closure $closure, array $params): void
|
||||
{
|
||||
$event = $this->getEvent($name);
|
||||
if (empty($event)) {
|
||||
if (!is_callable($closure)) {
|
||||
return;
|
||||
}
|
||||
call_user_func($closure, ...$params);
|
||||
} else {
|
||||
call_user_func($event, ...$params);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ use Server\ExceptionHandlerDispatcher;
|
||||
use Server\ExceptionHandlerInterface;
|
||||
use Server\SInterface\OnReceive;
|
||||
|
||||
abstract class Tcp implements OnReceive
|
||||
abstract class Tcp extends Server implements OnReceive
|
||||
{
|
||||
|
||||
use EventDispatchHelper;
|
||||
|
||||
@@ -12,7 +12,11 @@ use Server\ExceptionHandlerInterface;
|
||||
use Server\SInterface\OnPacket;
|
||||
use Server\SInterface\OnReceive;
|
||||
|
||||
abstract class Udp implements OnPacket
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
abstract class Udp extends Server implements OnPacket
|
||||
{
|
||||
|
||||
use EventDispatchHelper;
|
||||
|
||||
@@ -15,13 +15,12 @@ use Server\SInterface\OnHandshake;
|
||||
use Server\SInterface\OnMessage;
|
||||
use Swoole\Http\Request;
|
||||
use Swoole\Http\Response;
|
||||
use Swoole\Server;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
abstract class Websocket implements OnHandshake, OnMessage, OnClose
|
||||
abstract class Websocket extends Server implements OnHandshake, OnMessage, OnClose
|
||||
{
|
||||
|
||||
use EventDispatchHelper;
|
||||
@@ -58,7 +57,6 @@ abstract class Websocket implements OnHandshake, OnMessage, OnClose
|
||||
public function onHandshake(Request $request, Response $response): void
|
||||
{
|
||||
// TODO: Implement OnHandshake() method.
|
||||
/** @var \Swoole\WebSocket\Server $server */
|
||||
$secWebSocketKey = $request->header['sec-websocket-key'];
|
||||
$patten = '#^[+/0-9A-Za-z]{21}[AQgw]==$#';
|
||||
if (0 === preg_match($patten, $secWebSocketKey) || 16 !== strlen(base64_decode($secWebSocketKey))) {
|
||||
|
||||
@@ -1,161 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Exception;
|
||||
use HttpServer\Exception\RequestException;
|
||||
use HttpServer\Http\Request as HSRequest;
|
||||
use HttpServer\Route\Node;
|
||||
use HttpServer\Route\Router;
|
||||
use ReflectionException;
|
||||
use Server\Constrict\Response as CResponse;
|
||||
use Server\Constrict\ResponseEmitter;
|
||||
use Server\Events\OnAfterRequest;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Events\EventDispatch;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Exception\NotFindClassException;
|
||||
use Kiri\Kiri;
|
||||
use Swoole\Error;
|
||||
use Swoole\Http\Request;
|
||||
use Swoole\Http\Response;
|
||||
use Swoole\Server;
|
||||
use Swoole\Server\Port;
|
||||
use Throwable;
|
||||
|
||||
|
||||
/**
|
||||
* Class HTTPServerListener
|
||||
* @package Server
|
||||
*/
|
||||
class HTTPServerListener extends Abstracts\Server
|
||||
{
|
||||
|
||||
protected static bool|Port $_http;
|
||||
|
||||
use ListenerHelper;
|
||||
|
||||
/** @var Router|mixed */
|
||||
#[Inject('router')]
|
||||
public Router $router;
|
||||
|
||||
|
||||
/** @var CResponse|mixed */
|
||||
#[Inject(CResponse::class)]
|
||||
public CResponse $response;
|
||||
|
||||
|
||||
/** @var EventDispatch */
|
||||
#[Inject(EventDispatch::class)]
|
||||
public EventDispatch $eventDispatch;
|
||||
|
||||
|
||||
|
||||
#[Inject(ResponseEmitter::class)]
|
||||
public ResponseEmitter $responseEmitter;
|
||||
|
||||
|
||||
/**
|
||||
* @var ExceptionHandlerInterface
|
||||
*/
|
||||
public ExceptionHandlerInterface $exceptionHandler;
|
||||
|
||||
|
||||
/**
|
||||
* @throws ReflectionException
|
||||
* @throws ConfigException
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* UDPServerListener constructor.
|
||||
* @param Server|Port $server
|
||||
* @param array|null $settings
|
||||
* @return Server|Port
|
||||
* @throws Exception
|
||||
*/
|
||||
public function bindCallback(Server|Port $server, ?array $settings = []): Server|Port
|
||||
{
|
||||
$this->setEvents(Constant::CONNECT, $settings['events'][Constant::CONNECT] ?? null);
|
||||
|
||||
$server->set(array_merge($settings['settings'] ?? [], ['enable_unsafe_event' => false]));
|
||||
if (isset($settings['events'][Constant::REQUEST])) {
|
||||
$event = $settings['events'][Constant::REQUEST];
|
||||
if (is_array($event) && is_string($event[0])) {
|
||||
$event[0] = di($event[0]);
|
||||
}
|
||||
$server->on('request', $event);
|
||||
} else {
|
||||
$server->on('request', [$this, 'onRequest']);
|
||||
}
|
||||
$server->on('connect', [$this, 'onConnect']);
|
||||
$server->on('close', [$this, 'onClose']);
|
||||
return $server;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onConnect(Server $server, int $fd)
|
||||
{
|
||||
$this->runEvent(Constant::CONNECT, null, [$server, $fd]);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onRequest(Request $request, Response $response)
|
||||
{
|
||||
try {
|
||||
$node = $this->router->Branch_search(Constrict\Request::create($request));
|
||||
if (!($node instanceof Node)) {
|
||||
throw new RequestException('<h2>HTTP 404 Not Found</h2><hr><i>Powered by Swoole</i>', 404);
|
||||
}
|
||||
if (!(($responseData = $node->dispatch()) instanceof ResponseInterface)) {
|
||||
$responseData = $this->response->setContent($responseData)->setStatusCode(200);
|
||||
}
|
||||
} catch (Error | Throwable $exception) {
|
||||
$responseData = $this->exceptionHandler->emit($exception, $this->response);
|
||||
} finally {
|
||||
$this->responseEmitter->sender($response, $responseData);
|
||||
$this->eventDispatch->dispatch(new OnAfterRequest());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onDisconnect(Server $server, int $fd)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onClose(Server $server, int $fd)
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,45 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server;
|
||||
|
||||
|
||||
use Closure;
|
||||
use ReflectionException;
|
||||
use Kiri\Exception\NotFindClassException;
|
||||
use Kiri\Kiri;
|
||||
|
||||
|
||||
/**
|
||||
* Trait ListenerHelper
|
||||
* @package Server
|
||||
*/
|
||||
trait ListenerHelper
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array $events
|
||||
* @param array|Closure $default
|
||||
* @return mixed
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
protected static function callback(string $name, array $events, array|Closure $default): mixed
|
||||
{
|
||||
if (!is_array($events) || !isset($events[$name])) {
|
||||
return $default;
|
||||
}
|
||||
|
||||
$callback = $events[$name];
|
||||
if ($callback instanceof Closure) {
|
||||
return $callback;
|
||||
}
|
||||
$object = Kiri::getDi()->getReflect($callback[0]);
|
||||
if ($object->getMethod($callback[1])->isStatic()) {
|
||||
return $callback;
|
||||
}
|
||||
return [$object->newInstance(), $callback[1]];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -11,8 +11,6 @@ use Kiri\Kiri;
|
||||
use ReflectionException;
|
||||
use Server\Manager\OnPipeMessage;
|
||||
use Server\SInterface\CustomProcess;
|
||||
use Server\SInterface\OnClose;
|
||||
use Server\SInterface\OnConnect;
|
||||
use Server\SInterface\TaskExecute;
|
||||
use Server\Task\OnServerTask;
|
||||
use Swoole\Http\Server as HServer;
|
||||
@@ -26,7 +24,7 @@ use Swoole\WebSocket\Server as WServer;
|
||||
* Class OnServerManager
|
||||
* @package HttpServer\Service
|
||||
*/
|
||||
class ServerManager extends Abstracts\Server
|
||||
class ServerManager
|
||||
{
|
||||
|
||||
/** @var string */
|
||||
@@ -41,7 +39,7 @@ class ServerManager extends Abstracts\Server
|
||||
public int $mode = SWOOLE_TCP;
|
||||
|
||||
|
||||
private Server|WServer|HServer|null $server = null;
|
||||
private mixed $server = null;
|
||||
|
||||
|
||||
private static ?ServerManager $BASEServerListener = null;
|
||||
@@ -345,17 +343,6 @@ class ServerManager extends Abstracts\Server
|
||||
foreach ($events as $name => $event) {
|
||||
if (is_array($event) && is_string($event[0])) {
|
||||
$event[0] = Kiri::getDi()->get($event[0], [$server]);
|
||||
// if ($event[0] instanceof OnConnect && $server->getCallback('connect') != null) {
|
||||
// $server->on('connect', [$event[0], 'onConnect']);
|
||||
// }
|
||||
// if ($event[0] instanceof OnClose) {
|
||||
// if ($server->getCallback('disconnect') != null) {
|
||||
// $server->on('disconnect', [$event[0], 'onDisconnect']);
|
||||
// }
|
||||
// if ($server->getCallback('close') != null) {
|
||||
// $server->on('close', [$event[0], 'onClose']);
|
||||
// }
|
||||
// }
|
||||
}
|
||||
$server->on($name, $event);
|
||||
}
|
||||
|
||||
@@ -1,93 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Exception;
|
||||
use Server\Events\OnAfterRequest;
|
||||
use Kiri\Event;
|
||||
use Kiri\Events\EventDispatch;
|
||||
use Swoole\Server;
|
||||
use Swoole\Server\Port;
|
||||
|
||||
|
||||
/**
|
||||
* Class TCPServerListener
|
||||
* @package HttpServer\Service
|
||||
*/
|
||||
class TCPServerListener extends Abstracts\Server
|
||||
{
|
||||
|
||||
use ListenerHelper;
|
||||
|
||||
protected static bool|Port $_tcp;
|
||||
|
||||
|
||||
/** @var EventDispatch */
|
||||
#[Inject(EventDispatch::class)]
|
||||
public EventDispatch $eventDispatch;
|
||||
|
||||
|
||||
/**
|
||||
* UDPServerListener constructor.
|
||||
* @param Server|Port $server
|
||||
* @param array|null $settings
|
||||
* @return Server|Port
|
||||
* @throws Exception
|
||||
*/
|
||||
public function bindCallback(Server|Port $server, ?array $settings = []): Server|Port
|
||||
{
|
||||
$this->setEvents(Constant::CLOSE, $settings['events'][Constant::CLOSE] ?? null);
|
||||
$this->setEvents(Constant::RECEIVE, $settings['events'][Constant::RECEIVE] ?? null);
|
||||
$this->setEvents(Constant::CONNECT, $settings['events'][Constant::CONNECT] ?? null);
|
||||
|
||||
$server->set($settings['settings'] ?? []);
|
||||
$server->on('receive', [$this, 'onReceive']);
|
||||
$server->on('connect', [$this, 'onConnect']);
|
||||
$server->on('close', [$this, 'onClose']);
|
||||
return $server;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onConnect(Server $server, int $fd)
|
||||
{
|
||||
$this->runEvent(Constant::CONNECT, null, [$server, $fd]);
|
||||
|
||||
$this->eventDispatch->dispatch(new OnAfterRequest());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
* @param int $reactor_id
|
||||
* @param string $data
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onReceive(Server $server, int $fd, int $reactor_id, string $data)
|
||||
{
|
||||
$this->runEvent(Constant::RECEIVE, null, [$server, $fd, $reactor_id, $data]);
|
||||
|
||||
$this->eventDispatch->dispatch(new OnAfterRequest());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onClose(Server $server, int $fd)
|
||||
{
|
||||
$this->runEvent(Constant::CLOSE, null, [$server, $fd]);
|
||||
|
||||
|
||||
$this->eventDispatch->dispatch(new OnAfterRequest());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,62 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Exception;
|
||||
use ReflectionException;
|
||||
use Server\Events\OnAfterRequest;
|
||||
use Kiri\Event;
|
||||
use Kiri\Events\EventDispatch;
|
||||
use Kiri\Exception\NotFindClassException;
|
||||
use Swoole\Server;
|
||||
use Swoole\Server\Port;
|
||||
|
||||
|
||||
/**
|
||||
* Class UDPServerListener
|
||||
* @package HttpServer\Service
|
||||
*/
|
||||
class UDPServerListener extends Abstracts\Server
|
||||
{
|
||||
|
||||
|
||||
/** @var EventDispatch */
|
||||
#[Inject(EventDispatch::class)]
|
||||
public EventDispatch $eventDispatch;
|
||||
|
||||
|
||||
use ListenerHelper;
|
||||
|
||||
|
||||
/**
|
||||
* @param Server|Port $server
|
||||
* @param array|null $settings
|
||||
* @return Server|Port
|
||||
* @throws Exception
|
||||
*/
|
||||
public function bindCallback(Server|Port $server, ?array $settings = []): Server|Port
|
||||
{
|
||||
$this->setEvents(Constant::PACKET, $settings['events'][Constant::PACKET] ?? null);
|
||||
|
||||
$server->set($settings['settings'] ?? []);
|
||||
$server->on('packet', [$this, 'onPacket']);
|
||||
|
||||
return $server;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param string $data
|
||||
* @param array $clientInfo
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onPacket(Server $server, string $data, array $clientInfo)
|
||||
{
|
||||
$this->runEvent(Constant::MESSAGE, null, [$server, $data, $clientInfo]);
|
||||
|
||||
$this->eventDispatch->dispatch(new OnAfterRequest());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,164 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Server;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Exception;
|
||||
use ReflectionException;
|
||||
use Server\Events\OnAfterRequest;
|
||||
use Kiri\Events\EventDispatch;
|
||||
use Kiri\Exception\NotFindClassException;
|
||||
use Kiri\Kiri;
|
||||
use Swoole\Http\Request;
|
||||
use Swoole\Http\Response;
|
||||
use Swoole\Server;
|
||||
use Swoole\Server\Port;
|
||||
use Swoole\WebSocket\Frame;
|
||||
|
||||
|
||||
/**
|
||||
* Class WebSocketServerListener
|
||||
* @package HttpServer\Service
|
||||
*/
|
||||
class WebSocketServerListener extends Abstracts\Server
|
||||
{
|
||||
|
||||
protected static Server\Port $_http;
|
||||
|
||||
use ListenerHelper;
|
||||
|
||||
|
||||
#[Inject(EventDispatch::class)]
|
||||
public EventDispatch $eventDispatch;
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $server
|
||||
* @param array|null $settings
|
||||
* @return Port|Server
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function bindCallback(Server|Port $server, ?array $settings = []): Server|Port
|
||||
{
|
||||
$this->setEvents(Constant::CONNECT, $settings['events'][Constant::CONNECT] ?? null);
|
||||
$this->setEvents(Constant::HANDSHAKE, $settings['events'][Constant::HANDSHAKE] ?? null);
|
||||
$this->setEvents(Constant::MESSAGE, $settings['events'][Constant::MESSAGE] ?? null);
|
||||
$this->setEvents(Constant::CLOSE, $settings['events'][Constant::CLOSE] ?? null);
|
||||
|
||||
$server->set($settings['settings'] ?? []);
|
||||
$server->on('connect', [$this, 'onConnect']);
|
||||
$server->on('handshake', [$this, 'onHandshake']);
|
||||
$server->on('message', [$this, 'onMessage']);
|
||||
$server->on('close', [$this, 'onClose']);
|
||||
|
||||
if (swoole_version() >= '4.7' && isset($settings['events'][Constant::DISCONNECT])) {
|
||||
$this->setEvents(Constant::DISCONNECT, $settings['events'][Constant::DISCONNECT]);
|
||||
$server->on('disconnect', [$this, 'onDisconnect']);
|
||||
}
|
||||
|
||||
$events = $settings['events'][Constant::REQUEST] ?? [];
|
||||
if (!empty($events) && is_array($events)) {
|
||||
$events[0] = Kiri::getDi()->get($events[0]);
|
||||
$server->on('request', $events);
|
||||
}
|
||||
return $server;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandshake(Request $request, Response $response)
|
||||
{
|
||||
/** @var \Swoole\WebSocket\Server $server */
|
||||
$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'])) {
|
||||
$headers['Sec-websocket-Protocol'] = $request->header['sec-websocket-protocol'];
|
||||
}
|
||||
foreach ($headers as $key => $val) {
|
||||
$response->setHeader($key, $val);
|
||||
}
|
||||
$this->runEvent(Constant::HANDSHAKE, fn() => $this->disconnect($request, $response), [$request, $response]);
|
||||
|
||||
$this->eventDispatch->dispatch(new OnAfterRequest());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $request
|
||||
* @param $response
|
||||
*/
|
||||
public function disconnect($request, $response)
|
||||
{
|
||||
$response->setStatusCode(502);
|
||||
$response->end();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onConnect(Server $server, int $fd)
|
||||
{
|
||||
$this->runEvent(Constant::CONNECT, fn() => $server->confirm($fd), [$server, $fd]);
|
||||
|
||||
$this->eventDispatch->dispatch(new OnAfterRequest());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param \Swoole\WebSocket\Server $server
|
||||
* @param Frame $frame
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onMessage(\Swoole\WebSocket\Server $server, Frame $frame)
|
||||
{
|
||||
$this->runEvent(Constant::MESSAGE, fn() => $server->push($frame->fd, '.'), [$server, $frame]);
|
||||
|
||||
$this->eventDispatch->dispatch(new OnAfterRequest());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onClose(Server $server, int $fd)
|
||||
{
|
||||
$this->runEvent(Constant::CLOSE, fn() => $server->confirm($fd), [$server, $fd]);
|
||||
|
||||
$this->eventDispatch->dispatch(new OnAfterRequest());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onDisconnect(Server $server, int $fd)
|
||||
{
|
||||
$this->runEvent(Constant::DISCONNECT, fn() => $server->confirm($fd), [$server, $fd]);
|
||||
|
||||
$this->eventDispatch->dispatch(new OnAfterRequest());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,104 +0,0 @@
|
||||
<?php
|
||||
|
||||
|
||||
use Server\Constant;
|
||||
use Server\HTTPServerListener;
|
||||
use Server\Manager\OnServerDefault;
|
||||
use Server\Manager\OnServerManager;
|
||||
use Server\TCPServerListener;
|
||||
use Server\UDPServerListener;
|
||||
use Server\WebSocketServerListener;
|
||||
use Server\Worker\OnServerWorker;
|
||||
|
||||
return [
|
||||
'server' => [
|
||||
'settings' => [
|
||||
'worker_num' => swoole_cpu_num(),
|
||||
'reactor_num' => swoole_cpu_num(),
|
||||
'dispatch_mode' => 3,
|
||||
'task_worker_num' => 1,
|
||||
'enable_coroutine' => true,
|
||||
'task_enable_coroutine' => true,
|
||||
'daemonize' => 0,
|
||||
'open_tcp_keepalive' => 1,
|
||||
'heartbeat_check_interval' => 60,
|
||||
'heartbeat_idle_time' => 600,
|
||||
'tcp_keepidle' => 3,
|
||||
'tcp_keepinterval' => 1,
|
||||
'tcp_keepcount' => 2,
|
||||
'max_wait_time' => 60,
|
||||
'reload_async' => true,
|
||||
'enable_delay_receive' => true,
|
||||
'tcp_fastopen' => 1,
|
||||
'tcp_defer_accept' => 1
|
||||
],
|
||||
'events' => [
|
||||
Constant::PIPE_MESSAGE => [OnServerDefault::class, 'onPipeMessage'],
|
||||
Constant::SHUTDOWN => [OnServerDefault::class, 'onShutdown'],
|
||||
Constant::WORKER_START => [OnServerWorker::class, 'onWorkerStart'],
|
||||
Constant::WORKER_ERROR => [OnServerWorker::class, 'onWorkerError'],
|
||||
Constant::WORKER_EXIT => [OnServerWorker::class, 'onWorkerExit'],
|
||||
Constant::WORKER_STOP => [OnServerWorker::class, 'onWorkerStop'],
|
||||
Constant::MANAGER_START => [OnServerManager::class, 'onManagerStart'],
|
||||
Constant::MANAGER_STOP => [OnServerManager::class, 'onManagerStop'],
|
||||
Constant::BEFORE_RELOAD => [OnServerDefault::class, 'onBeforeReload'],
|
||||
Constant::AFTER_RELOAD => [OnServerDefault::class, 'onAfterReload'],
|
||||
Constant::START => [OnServerDefault::class, 'onStart'],
|
||||
],
|
||||
'ports' => [
|
||||
[
|
||||
'type' => Constant::SERVER_TYPE_HTTP,
|
||||
'host' => '0.0.0.0',
|
||||
'port' => 9002,
|
||||
'mode' => SWOOLE_SOCK_TCP,
|
||||
'events' => [
|
||||
Constant::REQUEST => [HTTPServerListener::class, 'onRequest'],
|
||||
],
|
||||
'settings' => [
|
||||
'open_http_protocol' => true,
|
||||
'open_http2_protocol' => false,
|
||||
'http_parse_cookie' => true,
|
||||
'http_compression' => true,
|
||||
'http_compression_level' => 5,
|
||||
'enable_unsafe_event' => false,
|
||||
]
|
||||
],
|
||||
[
|
||||
'type' => Constant::SERVER_TYPE_WEBSOCKET,
|
||||
'host' => '0.0.0.0',
|
||||
'port' => 9001,
|
||||
'mode' => SWOOLE_SOCK_TCP,
|
||||
'settings' => [
|
||||
'open_http_protocol' => false,
|
||||
'open_http2_protocol' => false
|
||||
],
|
||||
'events' => [
|
||||
Constant::CONNECT => [WebSocketServerListener::class, 'onConnect'],
|
||||
Constant::HANDSHAKE => [WebSocketServerListener::class, 'onHandshake'],
|
||||
Constant::MESSAGE => [WebSocketServerListener::class, 'onMessage'],
|
||||
Constant::CLOSE => [WebSocketServerListener::class, 'onClose'],
|
||||
]
|
||||
],
|
||||
[
|
||||
'type' => Constant::SERVER_TYPE_TCP,
|
||||
'host' => '0.0.0.0',
|
||||
'port' => 9003,
|
||||
'mode' => SWOOLE_SOCK_TCP,
|
||||
'events' => [
|
||||
Constant::CONNECT => [TCPServerListener::class, 'onConnect'],
|
||||
Constant::RECEIVE => [TCPServerListener::class, 'onReceive'],
|
||||
Constant::CLOSE => [TCPServerListener::class, 'onClose'],
|
||||
]
|
||||
],
|
||||
[
|
||||
'type' => Constant::SERVER_TYPE_UDP,
|
||||
'host' => '0.0.0.0',
|
||||
'port' => 9004,
|
||||
'mode' => SWOOLE_SOCK_UDP,
|
||||
'events' => [
|
||||
Constant::PACKET => [UDPServerListener::class, 'onPacket'],
|
||||
]
|
||||
],
|
||||
]
|
||||
]
|
||||
];
|
||||
Reference in New Issue
Block a user