改名
This commit is contained in:
@@ -6,6 +6,7 @@ namespace Server\Abstracts;
|
||||
|
||||
use Closure;
|
||||
use Exception;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
|
||||
@@ -20,6 +21,19 @@ abstract class Server
|
||||
protected array $_events = [];
|
||||
|
||||
|
||||
protected Event $_event;
|
||||
|
||||
|
||||
/**
|
||||
* Server constructor.
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->_event = Snowflake::getApp('event');
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array|null $events
|
||||
|
||||
@@ -32,9 +32,6 @@ class HTTPServerListener extends Abstracts\Server
|
||||
private Router $router;
|
||||
|
||||
|
||||
private Event $_event;
|
||||
|
||||
|
||||
/**
|
||||
* HTTPServerListener constructor.
|
||||
* @throws Exception
|
||||
@@ -42,7 +39,7 @@ class HTTPServerListener extends Abstracts\Server
|
||||
public function __construct()
|
||||
{
|
||||
$this->router = Snowflake::getApp('router');
|
||||
$this->_event = Snowflake::getApp('event');
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -83,10 +80,13 @@ class HTTPServerListener extends Abstracts\Server
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onConnect(Server $server, int $fd)
|
||||
{
|
||||
$this->runEvent(Constant::CONNECT, null, [$server, $fd]);
|
||||
|
||||
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
|
||||
}
|
||||
|
||||
|
||||
@@ -104,6 +104,7 @@ class HTTPServerListener extends Abstracts\Server
|
||||
}
|
||||
$this->router->dispatch();
|
||||
} catch (ExitException | Error | Throwable $exception) {
|
||||
$response->setHeader('Content-Type', 'text/html; charset=utf-8');
|
||||
$response->status($exception->getCode() == 0 ? 500 : $exception->getCode());
|
||||
$response->end($exception->getMessage());
|
||||
} finally {
|
||||
@@ -115,20 +116,26 @@ class HTTPServerListener extends Abstracts\Server
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onDisconnect(Server $server, int $fd)
|
||||
{
|
||||
$this->runEvent(Constant::DISCONNECT, null, [$server, $fd]);
|
||||
|
||||
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onClose(Server $server, int $fd)
|
||||
{
|
||||
$this->runEvent(Constant::CLOSE, null, [$server, $fd]);
|
||||
|
||||
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace Server;
|
||||
|
||||
use Exception;
|
||||
use ReflectionException;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Server;
|
||||
@@ -62,20 +63,26 @@ class TCPServerListener extends Abstracts\Server
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
* @param int|null $reactor_id
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onDisconnect(Server $server, int $fd, ?int $reactor_id = null)
|
||||
{
|
||||
$this->runEvent(Constant::HANDSHAKE, null, [$server, $fd, $reactor_id]);
|
||||
|
||||
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onConnect(Server $server, int $fd)
|
||||
{
|
||||
$this->runEvent(Constant::CONNECT, null, [$server, $fd]);
|
||||
|
||||
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
|
||||
}
|
||||
|
||||
|
||||
@@ -84,20 +91,26 @@ class TCPServerListener extends Abstracts\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->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onClose(Server $server, int $fd)
|
||||
{
|
||||
$this->runEvent(Constant::CLOSE, null, [$server, $fd]);
|
||||
|
||||
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace Server;
|
||||
|
||||
use Exception;
|
||||
use ReflectionException;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Server;
|
||||
@@ -56,10 +57,13 @@ class UDPServerListener extends Abstracts\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->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace Server;
|
||||
|
||||
use Exception;
|
||||
use ReflectionException;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Http\Request;
|
||||
@@ -86,6 +87,8 @@ class WebSocketServerListener extends Abstracts\Server
|
||||
$response->setHeader($key, $val);
|
||||
}
|
||||
$this->runEvent(Constant::HANDSHAKE, fn() => $this->disconnect($request, $response), [$request, $response]);
|
||||
|
||||
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
|
||||
}
|
||||
|
||||
|
||||
@@ -103,30 +106,39 @@ class WebSocketServerListener extends Abstracts\Server
|
||||
/**
|
||||
* @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->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user