Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG-2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion src/socketio-server/src/Aspect/SessionAspect.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@

class SessionAspect extends AbstractAspect
{
// 要切入的类,可以多个,亦可通过 :: 标识到具体的某个方法,通过 * 可以模糊匹配
public $classes = [
'Hyperf\SocketIOServer\SocketIO::onClose',
'Hyperf\SocketIOServer\SocketIO::onOpen',
Expand Down
4 changes: 2 additions & 2 deletions src/socketio-server/src/SocketIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/
namespace Hyperf\SocketIOServer;

use Closure;
use Hyperf\Contract\OnCloseInterface;
use Hyperf\Contract\OnMessageInterface;
use Hyperf\Contract\OnOpenInterface;
Expand All @@ -28,7 +29,6 @@
use Swoole\Coroutine\Channel;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\Server;
use Swoole\Timer;
use Swoole\WebSocket\Frame;

Expand Down Expand Up @@ -257,7 +257,7 @@ private function dispatch(int $fd, string $nsp, string $event, ...$payloads)

// Check if ack is required
$last = array_pop($payloads);
if ($last instanceof \Closure) {
if ($last instanceof Closure) {
$ack = $last;
} else {
array_push($payloads, $last);
Expand Down
2 changes: 1 addition & 1 deletion src/websocket-server/src/Security.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function isInvalidSecurityKey(string $key): bool
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',
Expand Down
15 changes: 9 additions & 6 deletions src/websocket-server/src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
use Swoole\WebSocket\CloseFrame;
use Swoole\WebSocket\Frame;
use Swoole\WebSocket\Server as WebSocketServer;
use Throwable;

class Server implements MiddlewareInitializerInterface, OnHandShakeInterface, OnCloseInterface, OnMessageInterface
{
Expand Down Expand Up @@ -141,7 +142,7 @@ public function onHandShake(SwooleRequest $request, SwooleResponse $response): v
$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));

Expand All @@ -151,9 +152,9 @@ public function onHandShake(SwooleRequest $request, SwooleResponse $response): v
}

$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);
Expand Down Expand Up @@ -196,7 +197,7 @@ public function onHandShake(SwooleRequest $request, SwooleResponse $response): v
$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 {
Expand Down Expand Up @@ -235,18 +236,20 @@ public function onMessage($server, Frame $frame): void

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);
Expand Down Expand Up @@ -279,7 +282,7 @@ protected function initRequest(SwooleRequest $request): ServerRequestInterface
/**
* Initialize PSR-7 Response.
*/
protected function initResponse(SwooleResponse $response): ResponseInterface
protected function initResponse(): ResponseInterface
{
Context::set(ResponseInterface::class, $psr7Response = new Psr7Response());
return $psr7Response;
Expand Down