Skip to content

Commit ece35ff

Browse files
Don't record the close message when the server is not websocket server. (#2182)
* fix #2180 * fix typo && remove unnecessary qualifier * Deleted useless code. * Update CHANGELOG-2.0.md Co-authored-by: 李铭昕 <[email protected]>
1 parent 8438c2d commit ece35ff

File tree

2 files changed

+10
-7
lines changed

2 files changed

+10
-7
lines changed

src/Security.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function isInvalidSecurityKey(string $key): bool
2828
return preg_match(self::PATTEN, $key) === 0 || strlen(base64_decode($key)) !== 16;
2929
}
3030

31-
public function handShakeHeaders(string $key): array
31+
public function handshakeHeaders(string $key): array
3232
{
3333
return [
3434
'Upgrade' => 'websocket',

src/Server.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
use Swoole\WebSocket\CloseFrame;
4646
use Swoole\WebSocket\Frame;
4747
use Swoole\WebSocket\Server as WebSocketServer;
48+
use Throwable;
4849

4950
class 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

Comments
 (0)