Skip to content

Commit 6a4240f

Browse files
authored
Added Swow support for hyperf/websocket-server. (#4873)
1 parent 292387b commit 6a4240f

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

src/Sender.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Psr\Log\LoggerInterface;
2020
use Swoole\Http\Response;
2121
use Swoole\Server;
22+
use Swow\Http\Server\Connection;
2223

2324
/**
2425
* @method push(int $fd, $data, int $opcode = null, $finish = null)
@@ -31,7 +32,7 @@ class Sender
3132
protected ?int $workerId = null;
3233

3334
/**
34-
* @var Response[]
35+
* @var Connection[]|Response[]
3536
*/
3637
protected array $responses = [];
3738

@@ -95,7 +96,10 @@ public function check($fd): bool
9596
return false;
9697
}
9798

98-
public function setResponse(int $fd, ?Response $response): void
99+
/**
100+
* @param null|Connection|Response $response
101+
*/
102+
public function setResponse(int $fd, mixed $response): void
99103
{
100104
if ($response === null) {
101105
unset($this->responses[$fd]);
@@ -104,7 +108,7 @@ public function setResponse(int $fd, ?Response $response): void
104108
}
105109
}
106110

107-
public function getResponse(int $fd): ?Response
111+
public function getResponse(int $fd): mixed
108112
{
109113
return $this->responses[$fd] ?? null;
110114
}

src/Server.php

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use Hyperf\Dispatcher\HttpDispatcher;
2525
use Hyperf\Engine\Constant;
2626
use Hyperf\Engine\Http\FdGetter;
27+
use Hyperf\Engine\Http\Server as HttpServer;
2728
use Hyperf\Engine\WebSocket\WebSocket;
2829
use Hyperf\ExceptionHandler\ExceptionHandlerDispatcher;
2930
use Hyperf\HttpMessage\Base\Response;
@@ -44,9 +45,11 @@
4445
use Psr\Container\ContainerInterface;
4546
use Psr\Http\Message\ResponseInterface;
4647
use Psr\Http\Message\ServerRequestInterface;
48+
use Swoole\Coroutine\Http\Server as SwCoServer;
4749
use Swoole\Http\Response as SwooleResponse;
4850
use Swoole\Server as SwooleServer;
4951
use Swoole\WebSocket\Server as WebSocketServer;
52+
use Swow\Http\Server\Connection;
5053
use Throwable;
5154

5255
class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, OnCloseInterface, OnMessageInterface
@@ -60,7 +63,7 @@ class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, On
6063
protected string $serverName = 'websocket';
6164

6265
/**
63-
* @var null|\Swoole\Coroutine\Http\Server|WebSocketServer
66+
* @var null|HttpServer|SwCoServer|WebSocketServer
6467
*/
6568
protected mixed $server = null;
6669

@@ -80,7 +83,7 @@ public function initCoreMiddleware(string $serverName): void
8083
]);
8184
}
8285

83-
public function getServer(): \Swoole\Coroutine\Http\Server|WebSocketServer
86+
public function getServer(): SwCoServer|WebSocketServer|HttpServer
8487
{
8588
if ($this->server) {
8689
return $this->server;
@@ -105,7 +108,7 @@ public function getSender(): Sender
105108

106109
/**
107110
* @param \Swoole\Http\Request|\Swow\Http\Server\Request $request
108-
* @param SwooleResponse $response
111+
* @param Connection|SwooleResponse $response
109112
*/
110113
public function onHandShake($request, $response): void
111114
{
@@ -149,8 +152,7 @@ public function onHandShake($request, $response): void
149152
if (Constant::isCoroutineServer($server)) {
150153
$upgrade = new WebSocket($response, $request);
151154

152-
// TODO: Support SWOW
153-
$response instanceof SwooleResponse && $this->getSender()->setResponse($fd, $response);
155+
$this->getSender()->setResponse($fd, $response);
154156
$this->deferOnOpen($request, $class, $response, $fd);
155157

156158
$upgrade->on(WebSocket::ON_MESSAGE, $this->getOnMessageCallback());
@@ -240,7 +242,7 @@ protected function getFd($response): int
240242
/**
241243
* @param mixed $request
242244
*/
243-
protected function deferOnOpen($request, string $class, SwooleResponse|WebSocketServer $server, int $fd)
245+
protected function deferOnOpen($request, string $class, SwooleResponse|WebSocketServer|Connection $server, int $fd)
244246
{
245247
$instance = $this->container->get($class);
246248
if ($server instanceof WebSocketServer) {

tests/ServerTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Hyperf\Utils\Reflection\ClassInvoker;
2121
use Hyperf\Utils\Waiter;
2222
use Hyperf\WebSocketServer\Server;
23+
use HyperfTest\WebSocketServer\Stub\FooServer;
2324
use HyperfTest\WebSocketServer\Stub\WebSocketStub;
2425
use Mockery;
2526
use PHPUnit\Framework\TestCase;
@@ -58,4 +59,10 @@ public function testDeferOnOpenInCoroutineStyleServer()
5859
$this->assertNotEquals(Coroutine::id(), WebSocketStub::$coroutineId);
5960
$this->assertFalse(\Swoole\Coroutine::exists(WebSocketStub::$coroutineId));
6061
}
62+
63+
public function testEngineServer()
64+
{
65+
$serv = new FooServer();
66+
$this->assertSame(1, $serv->getServer());
67+
}
6168
}

tests/Stub/FooServer.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace HyperfTest\WebSocketServer\Stub;
13+
14+
use Hyperf\Engine\Http\Server as HttpServer;
15+
16+
class FooServer
17+
{
18+
public function getServer(): HttpServer|int
19+
{
20+
return 1;
21+
}
22+
}

0 commit comments

Comments
 (0)