diff --git a/src/websocket-server/src/Context.php b/src/websocket-server/src/Context.php new file mode 100644 index 000000000..101e7d210 --- /dev/null +++ b/src/websocket-server/src/Context.php @@ -0,0 +1,88 @@ +yield(); - + Context::set(WsContext::FD, $request->fd); $security = $this->container->get(Security::class); $psr7Request = $this->initRequest($request); @@ -167,6 +168,7 @@ class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, On public function onMessage(WebSocketServer $server, Frame $frame): void { + Context::set(WsContext::FD, $frame->fd); $fdObj = FdCollector::get($frame->fd); if (! $fdObj) { $this->logger->warning(sprintf('WebSocket: fd[%d] does not exist.', $frame->fd)); @@ -187,6 +189,7 @@ class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, On { $this->logger->debug(sprintf('WebSocket: fd[%d] closed.', $fd)); + Context::set(WsContext::FD, $fd); $fdObj = FdCollector::get($fd); if (! $fdObj) { return; @@ -197,6 +200,7 @@ class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, On } FdCollector::del($fd); + WsContext::release($fd); } /** @@ -205,6 +209,7 @@ class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, On protected function initRequest(SwooleRequest $request): RequestInterface { Context::set(ServerRequestInterface::class, $psr7Request = Psr7Request::loadFromSwooleRequest($request)); + WsContext::set(ServerRequestInterface::class, $psr7Request); return $psr7Request; }