Skip to content

Commit 292387b

Browse files
authored
Fixed bug that onOpen failed when using async style for websocket server. (#4871)
1 parent e598c65 commit 292387b

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

src/Server.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,12 +243,20 @@ protected function getFd($response): int
243243
protected function deferOnOpen($request, string $class, SwooleResponse|WebSocketServer $server, int $fd)
244244
{
245245
$instance = $this->container->get($class);
246-
wait(static function () use ($request, $instance, $server, $fd) {
247-
Context::set(WsContext::FD, $fd);
248-
if ($instance instanceof OnOpenInterface) {
249-
$instance->onOpen($server, $request);
250-
}
251-
});
246+
if ($server instanceof WebSocketServer) {
247+
defer(static function () use ($request, $instance, $server) {
248+
if ($instance instanceof OnOpenInterface) {
249+
$instance->onOpen($server, $request);
250+
}
251+
});
252+
} else {
253+
wait(static function () use ($request, $instance, $server, $fd) {
254+
Context::set(WsContext::FD, $fd);
255+
if ($instance instanceof OnOpenInterface) {
256+
$instance->onOpen($server, $request);
257+
}
258+
});
259+
}
252260
}
253261

254262
/**

0 commit comments

Comments
 (0)