Skip to content

Commit

Permalink
Fixed websocket recv timeout. Added failed log. (#29)
Browse files Browse the repository at this point in the history
Co-authored-by: 李铭昕 <[email protected]>
  • Loading branch information
xuanyanwow and limingxinleo authored Apr 9, 2024
1 parent b3e1a02 commit 5617f01
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
matrix:
os: [ ubuntu-latest ]
php-version: [ '8.1', '8.2', '8.3' ]
swoole-version: [ 'v5.0.3', 'v5.1.1', 'master' ]
swoole-version: [ 'v5.0.3', 'v5.1.2', 'master' ]
exclude:
- php-version: '8.3'
swoole-version: 'v5.0.3'
Expand Down
2 changes: 1 addition & 1 deletion src/Coroutine.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public static function stats(): array
return SwooleCo::stats();
}

public static function exists(int $id = null): bool
public static function exists(?int $id = null): bool
{
return SwooleCo::exists($id);
}
Expand Down
16 changes: 14 additions & 2 deletions src/WebSocket/WebSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
namespace Hyperf\Engine\WebSocket;

use Hyperf\Engine\Contract\WebSocket\WebSocketInterface;
use Psr\Log\LoggerInterface;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\WebSocket\CloseFrame;
Expand All @@ -27,7 +28,7 @@ class WebSocket implements WebSocketInterface
*/
protected array $events = [];

public function __construct(Response $connection, Request $request)
public function __construct(Response $connection, Request $request, protected ?LoggerInterface $logger = null)
{
$this->connection = $connection;
$this->connection->upgrade();
Expand All @@ -42,7 +43,18 @@ public function start(): void
{
while (true) {
/** @var false|string|SwFrame $frame */
$frame = $this->connection->recv();
$frame = $this->connection->recv(-1);
if ($frame === false) {
$this->logger?->warning(
sprintf(
'%s:(%s) %s',
'Websocket recv failed:',
swoole_last_error(),
swoole_strerror(swoole_last_error(), 9)
)
);
}

if ($frame === false || $frame instanceof CloseFrame || $frame === '') {
if ($callback = $this->events[static::ON_CLOSE] ?? null) {
$callback($this->connection, $this->connection->fd);
Expand Down

0 comments on commit 5617f01

Please sign in to comment.