diff --git a/core/Websocket.php b/core/Websocket.php index 7c01af2..cbe2a75 100644 --- a/core/Websocket.php +++ b/core/Websocket.php @@ -5,6 +5,7 @@ namespace Coroutine\Server; use Kiri\Di\Context; use Kiri\Di\Inject\Container; use Kiri\Router\Constrict\ConstrictRequest; +use Kiri\Router\Router; use Psr\Http\Message\RequestInterface; use Swoole\Coroutine; use Swoole\Coroutine\Http\Server; @@ -38,16 +39,12 @@ abstract class Websocket $server = new Server($config->host, $config->port, false); $this->configureServer($server); - $server->handle($config->normalizePath($config->websocketPath), fn(Request $request, Response $ws) => $this->handler($request, $ws)); - if ($config->exposeOnlineLists) { - $server->handle($config->normalizePath($config->onlineListPath), fn(Request $request, Response $ws) => $this->getLists($request, $ws)); - } + $server->handle('/', fn(Request $request, Response $ws) => $this->handler($request, $ws)); echo 'websocket server start at ' . $config->host . ':' . $config->port . PHP_EOL; $server->start(); } - /** * @param Request $request * @param Response $ws @@ -86,8 +83,7 @@ abstract class Websocket public function handler(Request $request, Response $ws): void { $upgraded = false; - $userId = null; - $fd = null; + $userId = null; try { $psr7Request = $this->authorizeConnection($request); @@ -97,10 +93,10 @@ abstract class Websocket } $upgraded = true; - $userId = $psr7Request->getAuthority()->getUniqueId(); + $userId = $psr7Request->getAuthority()->getUniqueId(); $connection = new Struct($psr7Request->getAuthority(), $request, $ws); - $fd = $connection->fd; + $fd = $connection->fd; $this->transport->add($userId, $connection); defer(function () use ($userId, $fd) { @@ -197,8 +193,8 @@ abstract class Websocket { if ($this->config->maxCoroutine > 0 && class_exists(Coroutine::class)) { Coroutine::set([ - 'max_coroutine' => $this->config->maxCoroutine, - ]); + 'max_coroutine' => $this->config->maxCoroutine, + ]); } } @@ -259,22 +255,16 @@ abstract class Websocket } $message = match ($status) { - 400 => 'Bad Request', - 401 => 'Unauthorized', - 403 => 'Forbidden', - 404 => 'Not Found', - 409 => 'Conflict', - 413 => 'Payload Too Large', + 400 => 'Bad Request', + 401 => 'Unauthorized', + 403 => 'Forbidden', + 404 => 'Not Found', + 409 => 'Conflict', + 413 => 'Payload Too Large', default => 'Internal Server Error', }; - error_log(sprintf( - '[%s] %s in %s:%d', - static::class, - $throwable->getMessage(), - $throwable->getFile(), - $throwable->getLine(), - )); + error_log(sprintf('[%s] %s in %s:%d', static::class, $throwable->getMessage(), $throwable->getFile(), $throwable->getLine())); if ($upgraded) { @$ws->close();