Skip to content

Commit

Permalink
Add active connection detection and update log date format
Browse files Browse the repository at this point in the history
Added a function to actively detect live WebSocket connections in the 'hyperf.php' and 'WebSocketDebugger.php' files, which helps to
  • Loading branch information
AuroraYolo committed Aug 18, 2023
1 parent 7105da4 commit 6598189
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
1 change: 1 addition & 0 deletions bin/hyperf.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function initialize(): void
$debugger = WebSocketDebugger::createWithWebSocket('sdb', $serverConfig, $sslConfig);

Coroutine::run(fn () => $debugger?->start());
Coroutine::run(fn () => $debugger?->detectActiveConnections());
}

$application->run();
Expand Down
21 changes: 21 additions & 0 deletions cloud-admin/SDB/WebSocketDebugger.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
use Hyperf\Codec\Json;
use Hyperf\Contract\ConfigInterface;
use Hyperf\Redis\Pool\PoolFactory;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use ReflectionClass;
use RuntimeException;
use Swow\Channel;
Expand All @@ -40,6 +42,7 @@
use WeakMap;

use function CloudAdmin\Utils\di;
use function CloudAdmin\Utils\logger;
use function Swow\Debug\var_dump_return;

/**
Expand Down Expand Up @@ -413,4 +416,22 @@ public function out(string $string = '', bool $newline = true): static
));
return $this;
}

/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function detectActiveConnections(): void
{
while (true) {
foreach ($this->socket->getConnections() as $connection) {
try {
$connection->checkLiveness();
} catch (SocketException $exception) {
logger()->debug(sprintf('Client fd:[%s] is closed. Message:[%s]', $connection->getFd(), $exception->getMessage()));
}
}
sleep(20);
}
}
}
2 changes: 1 addition & 1 deletion config/autoload/logger.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
'class' => Monolog\Formatter\LineFormatter::class,
'constructor' => [
'format' => "%datetime% [%channel%.%level_name%]: %message% %context% %extra%\n",
'dateFormat' => 'g:i:s A',
'dateFormat' => 'Y-m-d H:i:s',
'allowInlineLineBreaks' => true,
],
],
Expand Down

0 comments on commit 6598189

Please sign in to comment.