This commit is contained in:
2021-03-31 16:52:39 +08:00
parent 36bf25c6a8
commit 05edfdd247
2 changed files with 15 additions and 50 deletions
+9 -49
View File
@@ -33,9 +33,15 @@ class OnClose extends Callback
public function onHandler(Server $server, int $fd)
{
try {
$this->execute($server, $fd);
$clientInfo = $server->getClientInfo($fd);
$event = Snowflake::app()->getEvent();
if (!$event->exists(($name = $this->getName($clientInfo)))) {
return;
}
$event->trigger($name, [$fd, $server]);
} catch (\Throwable $exception) {
$this->addError($exception,'throwable');
$this->addError($exception, 'throwable');
} finally {
fire(Event::SYSTEM_RESOURCE_RELEASES);
logger()->insert();
@@ -43,59 +49,13 @@ class OnClose extends Callback
}
/**
* @param Server $server
* @param int $fd
* @throws ComponentException
* @throws Exception
*/
private function execute(Server $server, int $fd): void
{
if (!$this->isWebsocket($server, $fd)) {
$client = $server->getClientInfo($fd);
fire($this->name($client['server_port']), [$server, $fd]);
} else {
$this->loadNode($server, $fd);
}
}
/**
* @param $server_port
* @return string
*/
private function name($server_port): string
private function getName($server_port): string
{
return 'listen ' . $server_port . ' ' . Event::SERVER_CLIENT_CLOSE;
}
/**
* @param $server
* @param $fd
* @return bool
*/
private function isWebsocket($server, $fd): bool
{
return $server instanceof WebsocketServer && $server->isEstablished($fd);
}
/**
* @param $server
* @param $fd
* @return mixed
* @throws ComponentException
* @throws ConfigException
* @throws NotFindClassException
* @throws Exception
*/
private function loadNode($server, $fd): mixed
{
$query = Request::socketQuery((object)['fd' => $fd], Socket::CLOSE);
if (($node = router()->find_path($query)) !== null) {
return $node->dispatch($server, $fd);
}
return null;
}
}