Skip to content

Commit d1853af

Browse files
authored
Fixed bug that websocket does not works when exist more than one server in coroutine style. (#3465)
1 parent 30268fa commit d1853af

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

src/Server.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Hyperf\HttpServer\ResponseEmitter;
2929
use Hyperf\HttpServer\Router\Dispatched;
3030
use Hyperf\Server\Event;
31+
use Hyperf\Server\Server as AsyncStyleServer;
3132
use Hyperf\Server\ServerManager;
3233
use Hyperf\Utils\Context;
3334
use Hyperf\Utils\Coordinator\Constants;
@@ -94,6 +95,11 @@ class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, On
9495
*/
9596
protected $serverName = 'websocket';
9697

98+
/**
99+
* @var null|\Swoole\Coroutine\Http\Server|WebSocketServer
100+
*/
101+
protected $server;
102+
97103
public function __construct(
98104
ContainerInterface $container,
99105
HttpDispatcher $dispatcher,
@@ -125,7 +131,20 @@ public function initCoreMiddleware(string $serverName): void
125131
*/
126132
public function getServer()
127133
{
128-
return $this->container->get(SwooleServer::class);
134+
if ($this->server) {
135+
return $this->server;
136+
}
137+
$config = $this->container->get(ConfigInterface::class);
138+
139+
$type = $config->get('server.type', AsyncStyleServer::class);
140+
141+
if ($type === AsyncStyleServer::class) {
142+
return $this->container->get(SwooleServer::class);
143+
}
144+
145+
[, $server] = ServerManager::get($this->serverName);
146+
147+
return $this->server = $server;
129148
}
130149

131150
public function getSender(): Sender

0 commit comments

Comments
 (0)