This commit is contained in:
2021-08-01 11:24:52 +08:00
parent a0f1d7d933
commit 7700c16b56
2 changed files with 21 additions and 5 deletions
+6 -5
View File
@@ -338,8 +338,10 @@ class ServerManager extends Abstracts\Server
$this->server->on('handshake', [$reflect, 'onHandshake']); $this->server->on('handshake', [$reflect, 'onHandshake']);
$this->server->on('message', [$reflect, 'onMessage']); $this->server->on('message', [$reflect, 'onMessage']);
$this->server->on('close', [$reflect, 'onClose']); $this->server->on('close', [$reflect, 'onClose']);
$this->server->on('disconnect', [$reflect, 'onDisconnect']);
$reflect->setEvents(Constant::HANDSHAKE, $settings['events'][Constant::HANDSHAKE] ?? null); $reflect->setEvents(Constant::HANDSHAKE, $settings['events'][Constant::HANDSHAKE] ?? null);
$reflect->setEvents(Constant::DISCONNECT, $settings['events'][Constant::DISCONNECT] ?? null);
$reflect->setEvents(Constant::MESSAGE, $settings['events'][Constant::MESSAGE] ?? null); $reflect->setEvents(Constant::MESSAGE, $settings['events'][Constant::MESSAGE] ?? null);
$reflect->setEvents(Constant::CONNECT, $settings['events'][Constant::CONNECT] ?? null); $reflect->setEvents(Constant::CONNECT, $settings['events'][Constant::CONNECT] ?? null);
$reflect->setEvents(Constant::CLOSE, $settings['events'][Constant::CLOSE] ?? null); $reflect->setEvents(Constant::CLOSE, $settings['events'][Constant::CLOSE] ?? null);
@@ -388,13 +390,12 @@ class ServerManager extends Abstracts\Server
*/ */
private function addCloseOrDisconnect($reflect, $settings): void private function addCloseOrDisconnect($reflect, $settings): void
{ {
if (swoole_version() >= '4.7.0') { if (swoole_version() >= '4.7') {
$this->server->on('disconnect', [$reflect, 'onDisconnect']);
$reflect->setEvents(Constant::DISCONNECT, $settings['events'][Constant::DISCONNECT] ?? null); $reflect->setEvents(Constant::DISCONNECT, $settings['events'][Constant::DISCONNECT] ?? null);
} else { $this->server->on('disconnect', [$reflect, 'onDisconnect']);
$this->server->on('close', [$reflect, 'onClose']);
$reflect->setEvents(Constant::CLOSE, $settings['events'][Constant::CLOSE] ?? null);
} }
$reflect->setEvents(Constant::CLOSE, $settings['events'][Constant::CLOSE] ?? null);
$this->server->on('close', [$reflect, 'onClose']);
} }
+15
View File
@@ -46,6 +46,7 @@ class WebSocketServerListener extends Abstracts\Server
$reflect = Snowflake::getDi()->getReflect(static::class)?->newInstance(); $reflect = Snowflake::getDi()->getReflect(static::class)?->newInstance();
$reflect->setEvents(Constant::CONNECT, $settings['events'][Constant::CONNECT] ?? null); $reflect->setEvents(Constant::CONNECT, $settings['events'][Constant::CONNECT] ?? null);
$reflect->setEvents(Constant::HANDSHAKE, $settings['events'][Constant::HANDSHAKE] ?? null); $reflect->setEvents(Constant::HANDSHAKE, $settings['events'][Constant::HANDSHAKE] ?? null);
$reflect->setEvents(Constant::DISCONNECT, $settings['events'][Constant::DISCONNECT] ?? null);
$reflect->setEvents(Constant::MESSAGE, $settings['events'][Constant::MESSAGE] ?? null); $reflect->setEvents(Constant::MESSAGE, $settings['events'][Constant::MESSAGE] ?? null);
$reflect->setEvents(Constant::CLOSE, $settings['events'][Constant::CLOSE] ?? null); $reflect->setEvents(Constant::CLOSE, $settings['events'][Constant::CLOSE] ?? null);
@@ -58,6 +59,7 @@ class WebSocketServerListener extends Abstracts\Server
static::$_http->on('connect', [$reflect, 'onConnect']); static::$_http->on('connect', [$reflect, 'onConnect']);
static::$_http->on('handshake', [$reflect, 'onHandshake']); static::$_http->on('handshake', [$reflect, 'onHandshake']);
static::$_http->on('message', [$reflect, 'onMessage']); static::$_http->on('message', [$reflect, 'onMessage']);
static::$_http->on('disconnect', [$reflect, 'onDisconnect']);
static::$_http->on('close', [$reflect, 'onClose']); static::$_http->on('close', [$reflect, 'onClose']);
return static::$_http; return static::$_http;
@@ -145,4 +147,17 @@ class WebSocketServerListener extends Abstracts\Server
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES); $this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
} }
/**
* @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->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
}
} }