modify
This commit is contained in:
+20
-20
@@ -142,13 +142,20 @@ class BASEServerListener
|
||||
*/
|
||||
private function addNewListener(string $type, string $host, int $port, int $mode, array $settings = [])
|
||||
{
|
||||
$match = match ($type) {
|
||||
self::SERVER_TYPE_TCP => TCPServerListener::class,
|
||||
self::SERVER_TYPE_UDP => UDPServerListener::class,
|
||||
self::SERVER_TYPE_HTTP => HTTPServerListener::class,
|
||||
self::SERVER_TYPE_WEBSOCKET => WebSocketServerListener::class
|
||||
};
|
||||
new $match($this->server, $host, $port, $mode, $settings);
|
||||
switch ($type) {
|
||||
case self::SERVER_TYPE_TCP:
|
||||
TCPServerListener::instance($this->server, $host, $port, $mode, $settings);
|
||||
break;
|
||||
case self::SERVER_TYPE_UDP:
|
||||
UDPServerListener::instance($this->server, $host, $port, $mode, $settings);
|
||||
break;
|
||||
case self::SERVER_TYPE_HTTP:
|
||||
HTTPServerListener::instance($this->server, $host, $port, $mode, $settings);
|
||||
break;
|
||||
case self::SERVER_TYPE_WEBSOCKET:
|
||||
WebSocketServerListener::instance($this->server, $host, $port, $mode, $settings);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -181,15 +188,15 @@ class BASEServerListener
|
||||
{
|
||||
if ($this->server->setting['task_worker_num'] > 0) $this->addTaskListener($settings['events']);
|
||||
if ($type === BASEServerListener::SERVER_TYPE_WEBSOCKET) {
|
||||
$this->server->on('handshake', $settings['events'][static::SERVER_ON_HANDSHAKE] ?? [$this, 'nullHasNeed']);
|
||||
$this->server->on('message', $settings['events'][static::SERVER_ON_MESSAGE] ?? [$this, 'nullHasNeed']);
|
||||
$this->server->on('close', $settings['events'][static::SERVER_ON_CLOSE] ?? [$this, 'nullHasNeed']);
|
||||
$this->server->on('handshake', $settings['events'][static::SERVER_ON_HANDSHAKE] ?? [WebSocketServerListener::class, 'onHandshake']);
|
||||
$this->server->on('message', $settings['events'][static::SERVER_ON_MESSAGE] ?? [WebSocketServerListener::class, 'onMessage']);
|
||||
$this->server->on('close', $settings['events'][static::SERVER_ON_CLOSE] ?? [WebSocketServerListener::class, 'onClose']);
|
||||
} else if ($type === BASEServerListener::SERVER_TYPE_UDP) {
|
||||
$this->server->on('packet', $settings['events'][static::SERVER_ON_PACKET] ?? [$this, 'nullHasNeed']);
|
||||
$this->server->on('packet', $settings['events'][static::SERVER_ON_PACKET] ?? [UDPServerListener::class, 'onPacket']);
|
||||
} else if ($type === BASEServerListener::SERVER_TYPE_HTTP) {
|
||||
$this->server->on('request', $settings['events'][static::SERVER_ON_REQUEST] ?? [$this, 'nullHasNeed']);
|
||||
$this->server->on('request', $settings['events'][static::SERVER_ON_REQUEST] ?? [HTTPServerListener::class, 'onRequest']);
|
||||
} else {
|
||||
$this->server->on('receive', $settings['events'][static::SERVER_ON_RECEIVE] ?? [$this, 'nullHasNeed']);
|
||||
$this->server->on('receive', $settings['events'][static::SERVER_ON_RECEIVE] ?? [TCPServerListener::class, 'onReceive']);
|
||||
}
|
||||
foreach ($settings['events'] as $event_type => $callback) {
|
||||
if ($this->server->getCallback($event_type) !== null) {
|
||||
@@ -213,13 +220,6 @@ class BASEServerListener
|
||||
}
|
||||
$this->server->on('finish', $events[static::SERVER_ON_FINISH] ?? [ServerTask::class, 'onFinish']);
|
||||
}
|
||||
|
||||
|
||||
public function nullHasNeed()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
+11
-12
@@ -8,7 +8,9 @@ use Swoole\Server;
|
||||
class HTTPServerListener
|
||||
{
|
||||
|
||||
protected mixed $_http;
|
||||
protected static mixed $_http;
|
||||
|
||||
use ListenerHelper;
|
||||
|
||||
|
||||
/**
|
||||
@@ -19,17 +21,14 @@ class HTTPServerListener
|
||||
* @param int $mode
|
||||
* @param array|null $settings
|
||||
*/
|
||||
public function __construct(mixed $server, string $host, int $port, int $mode, ?array $settings = [])
|
||||
public static function instance(mixed $server, string $host, int $port, int $mode, ?array $settings = [])
|
||||
{
|
||||
$this->_http = $server->addlistener($host, $port, $mode);
|
||||
$this->_http->set($settings['settings'] ?? []);
|
||||
static::$_http = $server->addlistener($host, $port, $mode);
|
||||
static::$_http->set($settings['settings'] ?? []);
|
||||
if ($server->getCallback('request') === null) {
|
||||
$server->on('request', $settings['events'][BASEServerListener::SERVER_ON_REQUEST] ?? [$this, 'onRequest']);
|
||||
}
|
||||
if (!in_array($server->setting['dispatch_mode'] ?? 2, [1, 3]) || $server->setting['enable_unsafe_event'] ?? false == true) {
|
||||
$this->_http->on('connect', $settings['events'][BASEServerListener::SERVER_ON_CONNECT] ?? [$this, 'onConnect']);
|
||||
$this->_http->on('close', $settings['events'][BASEServerListener::SERVER_ON_CLOSE] ?? [$this, 'onClose']);
|
||||
$server->on('request', $settings['events'][BASEServerListener::SERVER_ON_REQUEST] ?? [static::class, 'onRequest']);
|
||||
}
|
||||
static::onConnectAndClose($server, static::$_http);
|
||||
}
|
||||
|
||||
|
||||
@@ -37,7 +36,7 @@ class HTTPServerListener
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
*/
|
||||
public function onConnect(Server $server, int $fd)
|
||||
public static function onConnect(Server $server, int $fd)
|
||||
{
|
||||
var_dump(__FILE__ . ':' . __LINE__);
|
||||
}
|
||||
@@ -47,7 +46,7 @@ class HTTPServerListener
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
*/
|
||||
public function onRequest(Request $request, Response $response)
|
||||
public static function onRequest(Request $request, Response $response)
|
||||
{
|
||||
$response->setStatusCode(200);
|
||||
$response->end('');
|
||||
@@ -58,7 +57,7 @@ class HTTPServerListener
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
*/
|
||||
public function onClose(Server $server, int $fd)
|
||||
public static function onClose(Server $server, int $fd)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
|
||||
trait ListenerHelper
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param $server
|
||||
* @param $newServer
|
||||
*/
|
||||
public static function onConnectAndClose($server, $newServer)
|
||||
{
|
||||
if (in_array($server->setting['dispatch_mode'] ?? 2, [1, 3])){
|
||||
return;
|
||||
}
|
||||
if (!($server->setting['enable_unsafe_event'] ?? false)) {
|
||||
return;
|
||||
}
|
||||
$newServer->on('connect', $settings['events'][BASEServerListener::SERVER_ON_CONNECT] ?? [static::class, 'onConnect']);
|
||||
$newServer->on('close', $settings['events'][BASEServerListener::SERVER_ON_CLOSE] ?? [static::class, 'onClose']);
|
||||
}
|
||||
|
||||
}
|
||||
+43
-44
@@ -11,57 +11,56 @@ use Swoole\Server;
|
||||
class TCPServerListener
|
||||
{
|
||||
|
||||
protected mixed $_tcp;
|
||||
use ListenerHelper;
|
||||
|
||||
protected static mixed $_tcp;
|
||||
|
||||
|
||||
/**
|
||||
* UDPServerListener constructor.
|
||||
* @param Server $server
|
||||
* @param string $host
|
||||
* @param int $port
|
||||
* @param int $mode
|
||||
* @param array|null $settings
|
||||
*/
|
||||
public function __construct(Server $server, string $host, int $port, int $mode, ?array $settings = [])
|
||||
{
|
||||
$this->_tcp = $server->addlistener($host, $port, $mode);
|
||||
$this->_tcp->set($settings);
|
||||
$this->_tcp->on('receive', $settings['events'][BASEServerListener::SERVER_ON_RECEIVE] ?? [$this, 'onReceive']);
|
||||
if (!in_array($server->setting['dispatch_mode'] ?? 2, [1, 3]) || $server->setting['enable_unsafe_event'] ?? false == true) {
|
||||
$this->_tcp->on('connect', $settings['events'][BASEServerListener::SERVER_ON_CONNECT] ?? [$this, 'onConnect']);
|
||||
$this->_tcp->on('close', $settings['events'][BASEServerListener::SERVER_ON_CLOSE] ?? [$this, 'onClose']);
|
||||
}
|
||||
}
|
||||
/**
|
||||
* UDPServerListener constructor.
|
||||
* @param Server $server
|
||||
* @param string $host
|
||||
* @param int $port
|
||||
* @param int $mode
|
||||
* @param array|null $settings
|
||||
*/
|
||||
public static function instance(Server $server, string $host, int $port, int $mode, ?array $settings = [])
|
||||
{
|
||||
static::$_tcp = $server->addlistener($host, $port, $mode);
|
||||
static::$_tcp->set($settings);
|
||||
static::$_tcp->on('receive', $settings['events'][BASEServerListener::SERVER_ON_RECEIVE] ?? [static::class, 'onReceive']);
|
||||
static::onConnectAndClose($server, static::$_tcp);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
*/
|
||||
public function onConnect(Server $server, int $fd)
|
||||
{
|
||||
var_dump(__FILE__ . ':' . __LINE__);
|
||||
}
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
*/
|
||||
public static function onConnect(Server $server, int $fd)
|
||||
{
|
||||
var_dump(__FILE__ . ':' . __LINE__);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
* @param int $reactor_id
|
||||
* @param string $data
|
||||
*/
|
||||
public function onReceive(Server $server, int $fd, int $reactor_id, string $data)
|
||||
{
|
||||
$server->send($fd, $data);
|
||||
}
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
* @param int $reactor_id
|
||||
* @param string $data
|
||||
*/
|
||||
public static function onReceive(Server $server, int $fd, int $reactor_id, string $data)
|
||||
{
|
||||
$server->send($fd, $data);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
*/
|
||||
public function onClose(Server $server, int $fd)
|
||||
{
|
||||
}
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
*/
|
||||
public static function onClose(Server $server, int $fd)
|
||||
{
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -11,22 +11,21 @@ use Swoole\Server;
|
||||
class UDPServerListener
|
||||
{
|
||||
|
||||
protected mixed $_udp;
|
||||
protected static mixed $_udp;
|
||||
|
||||
|
||||
/**
|
||||
* UDPServerListener constructor.
|
||||
* @param Server $server
|
||||
* @param string $host
|
||||
* @param int $port
|
||||
* @param int $mode
|
||||
* @param array|null $settings
|
||||
*/
|
||||
public function __construct(Server $server, string $host, int $port, int $mode, ?array $settings = [])
|
||||
public static function instance(Server $server, string $host, int $port, int $mode, ?array $settings = [])
|
||||
{
|
||||
$this->_udp = $server->addlistener($host, $port, $mode);
|
||||
$this->_udp->set($settings['settings'] ?? []);
|
||||
$this->_udp->on('packet', $settings['events'][BASEServerListener::SERVER_ON_PACKET] ?? [$this, 'onPacket']);
|
||||
static::$_udp = $server->addlistener($host, $port, $mode);
|
||||
static::$_udp->set($settings['settings'] ?? []);
|
||||
static::$_udp->on('packet', $settings['events'][BASEServerListener::SERVER_ON_PACKET] ?? [static::class, 'onPacket']);
|
||||
}
|
||||
|
||||
|
||||
@@ -35,7 +34,7 @@ class UDPServerListener
|
||||
* @param string $data
|
||||
* @param array $clientInfo
|
||||
*/
|
||||
public function onPacket(Server $server, string $data, array $clientInfo)
|
||||
public static function onPacket(Server $server, string $data, array $clientInfo)
|
||||
{
|
||||
$server->sendto($clientInfo['address'], $clientInfo['port'], $data);
|
||||
}
|
||||
|
||||
@@ -13,7 +13,7 @@ use Swoole\WebSocket\Frame;
|
||||
class WebSocketServerListener
|
||||
{
|
||||
|
||||
protected mixed $_http;
|
||||
protected static mixed $_http;
|
||||
|
||||
|
||||
/**
|
||||
@@ -24,22 +24,22 @@ class WebSocketServerListener
|
||||
* @param int $mode
|
||||
* @param array|null $settings
|
||||
*/
|
||||
public function __construct(mixed $server, string $host, int $port, int $mode, ?array $settings = [])
|
||||
public static function instance(mixed $server, string $host, int $port, int $mode, ?array $settings = [])
|
||||
{
|
||||
$this->_http = $server->addlistener($host, $port, $mode);
|
||||
$this->_http->set($settings['settings'] ?? []);
|
||||
$this->_http->on('handshake', $settings['events'][BASEServerListener::SERVER_ON_HANDSHAKE] ?? [$this, 'onHandshake']);
|
||||
$this->_http->on('message', $settings['events'][BASEServerListener::SERVER_ON_MESSAGE] ?? [$this, 'onMessage']);
|
||||
$this->_http->on('connect', $settings['events'][BASEServerListener::SERVER_ON_CONNECT] ?? [$this, 'onConnect']);
|
||||
$this->_http->on('close', $settings['events'][BASEServerListener::SERVER_ON_CLOSE] ?? [$this, 'onClose']);
|
||||
}
|
||||
static::$_http = $server->addlistener($host, $port, $mode);
|
||||
static::$_http->set($settings['settings'] ?? []);
|
||||
static::$_http->on('handshake', $settings['events'][BASEServerListener::SERVER_ON_HANDSHAKE] ?? [static::class, 'onHandshake']);
|
||||
static::$_http->on('message', $settings['events'][BASEServerListener::SERVER_ON_MESSAGE] ?? [static::class, 'onMessage']);
|
||||
static::$_http->on('connect', $settings['events'][BASEServerListener::SERVER_ON_CONNECT] ?? [static::class, 'onConnect']);
|
||||
static::$_http->on('close', $settings['events'][BASEServerListener::SERVER_ON_CLOSE] ?? [static::class, 'onClose']);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
*/
|
||||
public function onHandshake(Request $request, Response $response)
|
||||
public static function onHandshake(Request $request, Response $response)
|
||||
{
|
||||
|
||||
}
|
||||
@@ -49,7 +49,7 @@ class WebSocketServerListener
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
*/
|
||||
public function onConnect(Server $server, int $fd)
|
||||
public static function onConnect(Server $server, int $fd)
|
||||
{
|
||||
var_dump(__FILE__ . ':' . __LINE__);
|
||||
}
|
||||
@@ -59,7 +59,7 @@ class WebSocketServerListener
|
||||
* @param \Swoole\WebSocket\Server|Server $server
|
||||
* @param Frame $frame
|
||||
*/
|
||||
public function onMessage(\Swoole\WebSocket\Server|Server $server, Frame $frame)
|
||||
public static function onMessage(\Swoole\WebSocket\Server|Server $server, Frame $frame)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ class WebSocketServerListener
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
*/
|
||||
public function onClose(Server $server, int $fd)
|
||||
public static function onClose(Server $server, int $fd)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user