4545use Swoole \WebSocket \CloseFrame ;
4646use Swoole \WebSocket \Frame ;
4747use Swoole \WebSocket \Server as WebSocketServer ;
48+ use Throwable ;
4849
4950class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, OnCloseInterface, OnMessageInterface
5051{
@@ -141,7 +142,7 @@ public function onHandShake(SwooleRequest $request, SwooleResponse $response): v
141142 $ security = $ this ->container ->get (Security::class);
142143
143144 $ psr7Request = $ this ->initRequest ($ request );
144- $ psr7Response = $ this ->initResponse ($ response );
145+ $ psr7Response = $ this ->initResponse ();
145146
146147 $ this ->logger ->debug (sprintf ('WebSocket: fd[%d] start a handshake request. ' , $ fd ));
147148
@@ -151,9 +152,9 @@ public function onHandShake(SwooleRequest $request, SwooleResponse $response): v
151152 }
152153
153154 $ psr7Request = $ this ->coreMiddleware ->dispatch ($ psr7Request );
155+ $ middlewares = $ this ->middlewares ;
154156 /** @var Dispatched $dispatched */
155157 $ dispatched = $ psr7Request ->getAttribute (Dispatched::class);
156- $ middlewares = $ this ->middlewares ;
157158 if ($ dispatched ->isFound ()) {
158159 $ registedMiddlewares = MiddlewareManager::get ($ this ->serverName , $ dispatched ->handler ->route , $ psr7Request ->getMethod ());
159160 $ middlewares = array_merge ($ middlewares , $ registedMiddlewares );
@@ -196,7 +197,7 @@ public function onHandShake(SwooleRequest $request, SwooleResponse $response): v
196197 $ this ->deferOnOpen ($ request , $ class , $ server );
197198 }
198199 }
199- } catch (\ Throwable $ throwable ) {
200+ } catch (Throwable $ throwable ) {
200201 // Delegate the exception to exception handler.
201202 $ psr7Response = $ this ->exceptionHandlerDispatcher ->dispatch ($ throwable , $ this ->exceptionHandlers );
202203 } finally {
@@ -235,18 +236,20 @@ public function onMessage($server, Frame $frame): void
235236
236237 public function onClose ($ server , int $ fd , int $ reactorId ): void
237238 {
238- $ this ->logger ->debug (sprintf ('WebSocket: fd[%d] closed. ' , $ fd ));
239-
240239 $ fdObj = FdCollector::get ($ fd );
241240 if (! $ fdObj ) {
242241 return ;
243242 }
243+
244+ $ this ->logger ->debug (sprintf ('WebSocket: fd[%d] closed. ' , $ fd ));
245+
244246 Context::set (WsContext::FD , $ fd );
245247 defer (function () use ($ fd ) {
246248 // Move those functions to defer, because onClose may throw exceptions
247249 FdCollector::del ($ fd );
248250 WsContext::release ($ fd );
249251 });
252+
250253 $ instance = $ this ->container ->get ($ fdObj ->class );
251254 if ($ instance instanceof OnCloseInterface) {
252255 $ instance ->onClose ($ server , $ fd , $ reactorId );
@@ -279,7 +282,7 @@ protected function initRequest(SwooleRequest $request): ServerRequestInterface
279282 /**
280283 * Initialize PSR-7 Response.
281284 */
282- protected function initResponse (SwooleResponse $ response ): ResponseInterface
285+ protected function initResponse (): ResponseInterface
283286 {
284287 Context::set (ResponseInterface::class, $ psr7Response = new Psr7Response ());
285288 return $ psr7Response ;
0 commit comments