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: 李铭昕 <715557344@qq.com>
This commit is contained in:
yansongda 2020-07-26 13:54:45 +08:00 committed by GitHub
parent bbf8ae4793
commit 911b73f3fe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 13 additions and 10 deletions

View File

@ -17,6 +17,7 @@
## Optimized
- [#2043](https://github.com/hyperf/hyperf/pull/2043) Throw an exception when none of the scan directories exists.
- [#2182](https://github.com/hyperf/hyperf/pull/2182) Don't record the close message when the server is not websocket server.
# v2.0.3 - 2020-07-20

View File

@ -20,7 +20,6 @@ use Psr\Http\Message\ServerRequestInterface;
class SessionAspect extends AbstractAspect
{
// 要切入的类,可以多个,亦可通过 :: 标识到具体的某个方法,通过 * 可以模糊匹配
public $classes = [
'Hyperf\SocketIOServer\SocketIO::onClose',
'Hyperf\SocketIOServer\SocketIO::onOpen',

View File

@ -11,6 +11,7 @@ declare(strict_types=1);
*/
namespace Hyperf\SocketIOServer;
use Closure;
use Hyperf\Contract\OnCloseInterface;
use Hyperf\Contract\OnMessageInterface;
use Hyperf\Contract\OnOpenInterface;
@ -28,7 +29,6 @@ use Hyperf\WebSocketServer\Sender;
use Swoole\Coroutine\Channel;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\Server;
use Swoole\Timer;
use Swoole\WebSocket\Frame;
@ -257,7 +257,7 @@ class SocketIO implements OnMessageInterface, OnOpenInterface, OnCloseInterface
// Check if ack is required
$last = array_pop($payloads);
if ($last instanceof \Closure) {
if ($last instanceof Closure) {
$ack = $last;
} else {
array_push($payloads, $last);

View File

@ -28,7 +28,7 @@ class Security
return preg_match(self::PATTEN, $key) === 0 || strlen(base64_decode($key)) !== 16;
}
public function handShakeHeaders(string $key): array
public function handshakeHeaders(string $key): array
{
return [
'Upgrade' => 'websocket',

View File

@ -45,6 +45,7 @@ use Swoole\Server as SwooleServer;
use Swoole\WebSocket\CloseFrame;
use Swoole\WebSocket\Frame;
use Swoole\WebSocket\Server as WebSocketServer;
use Throwable;
class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, OnCloseInterface, OnMessageInterface
{
@ -141,7 +142,7 @@ class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, On
$security = $this->container->get(Security::class);
$psr7Request = $this->initRequest($request);
$psr7Response = $this->initResponse($response);
$psr7Response = $this->initResponse();
$this->logger->debug(sprintf('WebSocket: fd[%d] start a handshake request.', $fd));
@ -151,9 +152,9 @@ class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, On
}
$psr7Request = $this->coreMiddleware->dispatch($psr7Request);
$middlewares = $this->middlewares;
/** @var Dispatched $dispatched */
$dispatched = $psr7Request->getAttribute(Dispatched::class);
$middlewares = $this->middlewares;
if ($dispatched->isFound()) {
$registedMiddlewares = MiddlewareManager::get($this->serverName, $dispatched->handler->route, $psr7Request->getMethod());
$middlewares = array_merge($middlewares, $registedMiddlewares);
@ -196,7 +197,7 @@ class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, On
$this->deferOnOpen($request, $class, $server);
}
}
} catch (\Throwable $throwable) {
} catch (Throwable $throwable) {
// Delegate the exception to exception handler.
$psr7Response = $this->exceptionHandlerDispatcher->dispatch($throwable, $this->exceptionHandlers);
} finally {
@ -235,18 +236,20 @@ class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, On
public function onClose($server, int $fd, int $reactorId): void
{
$this->logger->debug(sprintf('WebSocket: fd[%d] closed.', $fd));
$fdObj = FdCollector::get($fd);
if (! $fdObj) {
return;
}
$this->logger->debug(sprintf('WebSocket: fd[%d] closed.', $fd));
Context::set(WsContext::FD, $fd);
defer(function () use ($fd) {
// Move those functions to defer, because onClose may throw exceptions
FdCollector::del($fd);
WsContext::release($fd);
});
$instance = $this->container->get($fdObj->class);
if ($instance instanceof OnCloseInterface) {
$instance->onClose($server, $fd, $reactorId);
@ -279,7 +282,7 @@ class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, On
/**
* Initialize PSR-7 Response.
*/
protected function initResponse(SwooleResponse $response): ResponseInterface
protected function initResponse(): ResponseInterface
{
Context::set(ResponseInterface::class, $psr7Response = new Psr7Response());
return $psr7Response;