diff --git a/CHANGELOG-2.0.md b/CHANGELOG-2.0.md index e67922f79..e4102cef2 100644 --- a/CHANGELOG-2.0.md +++ b/CHANGELOG-2.0.md @@ -17,6 +17,7 @@ ## Optimized - [#2043](https://github.com/hyperf/hyperf/pull/2043) Throw an exception when none of the scan directories exists. +- [#2182](https://github.com/hyperf/hyperf/pull/2182) Don't record the close message when the server is not websocket server. # v2.0.3 - 2020-07-20 diff --git a/src/socketio-server/src/Aspect/SessionAspect.php b/src/socketio-server/src/Aspect/SessionAspect.php index 96a462c1c..d5c1d3abe 100644 --- a/src/socketio-server/src/Aspect/SessionAspect.php +++ b/src/socketio-server/src/Aspect/SessionAspect.php @@ -20,7 +20,6 @@ use Psr\Http\Message\ServerRequestInterface; class SessionAspect extends AbstractAspect { - // 要切入的类,可以多个,亦可通过 :: 标识到具体的某个方法,通过 * 可以模糊匹配 public $classes = [ 'Hyperf\SocketIOServer\SocketIO::onClose', 'Hyperf\SocketIOServer\SocketIO::onOpen', diff --git a/src/socketio-server/src/SocketIO.php b/src/socketio-server/src/SocketIO.php index eadf3dd63..08ca4afaf 100644 --- a/src/socketio-server/src/SocketIO.php +++ b/src/socketio-server/src/SocketIO.php @@ -11,6 +11,7 @@ declare(strict_types=1); */ namespace Hyperf\SocketIOServer; +use Closure; use Hyperf\Contract\OnCloseInterface; use Hyperf\Contract\OnMessageInterface; use Hyperf\Contract\OnOpenInterface; @@ -28,7 +29,6 @@ use Hyperf\WebSocketServer\Sender; use Swoole\Coroutine\Channel; use Swoole\Http\Request; use Swoole\Http\Response; -use Swoole\Server; use Swoole\Timer; use Swoole\WebSocket\Frame; @@ -257,7 +257,7 @@ class SocketIO implements OnMessageInterface, OnOpenInterface, OnCloseInterface // Check if ack is required $last = array_pop($payloads); - if ($last instanceof \Closure) { + if ($last instanceof Closure) { $ack = $last; } else { array_push($payloads, $last); diff --git a/src/websocket-server/src/Security.php b/src/websocket-server/src/Security.php index 09505d787..1209b20fc 100644 --- a/src/websocket-server/src/Security.php +++ b/src/websocket-server/src/Security.php @@ -28,7 +28,7 @@ class Security return preg_match(self::PATTEN, $key) === 0 || strlen(base64_decode($key)) !== 16; } - public function handShakeHeaders(string $key): array + public function handshakeHeaders(string $key): array { return [ 'Upgrade' => 'websocket', diff --git a/src/websocket-server/src/Server.php b/src/websocket-server/src/Server.php index a191107de..212793907 100644 --- a/src/websocket-server/src/Server.php +++ b/src/websocket-server/src/Server.php @@ -45,6 +45,7 @@ use Swoole\Server as SwooleServer; use Swoole\WebSocket\CloseFrame; use Swoole\WebSocket\Frame; use Swoole\WebSocket\Server as WebSocketServer; +use Throwable; class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, OnCloseInterface, OnMessageInterface { @@ -141,7 +142,7 @@ class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, On $security = $this->container->get(Security::class); $psr7Request = $this->initRequest($request); - $psr7Response = $this->initResponse($response); + $psr7Response = $this->initResponse(); $this->logger->debug(sprintf('WebSocket: fd[%d] start a handshake request.', $fd)); @@ -151,9 +152,9 @@ class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, On } $psr7Request = $this->coreMiddleware->dispatch($psr7Request); + $middlewares = $this->middlewares; /** @var Dispatched $dispatched */ $dispatched = $psr7Request->getAttribute(Dispatched::class); - $middlewares = $this->middlewares; if ($dispatched->isFound()) { $registedMiddlewares = MiddlewareManager::get($this->serverName, $dispatched->handler->route, $psr7Request->getMethod()); $middlewares = array_merge($middlewares, $registedMiddlewares); @@ -196,7 +197,7 @@ class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, On $this->deferOnOpen($request, $class, $server); } } - } catch (\Throwable $throwable) { + } catch (Throwable $throwable) { // Delegate the exception to exception handler. $psr7Response = $this->exceptionHandlerDispatcher->dispatch($throwable, $this->exceptionHandlers); } finally { @@ -235,18 +236,20 @@ class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, On public function onClose($server, int $fd, int $reactorId): void { - $this->logger->debug(sprintf('WebSocket: fd[%d] closed.', $fd)); - $fdObj = FdCollector::get($fd); if (! $fdObj) { return; } + + $this->logger->debug(sprintf('WebSocket: fd[%d] closed.', $fd)); + Context::set(WsContext::FD, $fd); defer(function () use ($fd) { // Move those functions to defer, because onClose may throw exceptions FdCollector::del($fd); WsContext::release($fd); }); + $instance = $this->container->get($fdObj->class); if ($instance instanceof OnCloseInterface) { $instance->onClose($server, $fd, $reactorId); @@ -279,7 +282,7 @@ class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, On /** * Initialize PSR-7 Response. */ - protected function initResponse(SwooleResponse $response): ResponseInterface + protected function initResponse(): ResponseInterface { Context::set(ResponseInterface::class, $psr7Response = new Psr7Response()); return $psr7Response;