Skip to content

Commit

Permalink
Update LabelerServer.php
Browse files Browse the repository at this point in the history
  • Loading branch information
puklipo committed Dec 26, 2024
1 parent ca1011a commit af60d41
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions src/Labeler/Server/LabelerServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ final class LabelerServer

protected int $count = 1;

protected bool $useHeartbeat = false;

public function start(?string $host = null, ?int $port = null): void
{
if (! is_null($host)) {
Expand Down Expand Up @@ -64,24 +66,26 @@ private function onWorkerStart(Worker $worker): void

$http->listen();

Timer::add(10, function () use ($worker) {
$time_now = time();
foreach ($worker->connections as $connection) {
if (empty($connection->lastMessageTime)) {
if ($this->useHeartbeat) {
Timer::add(10, function () use ($worker) {
$time_now = time();
foreach ($worker->connections as $connection) {
if (empty($connection->lastMessageTime)) {
/**
* @phpstan-ignore property.notFound
*/
$connection->lastMessageTime = $time_now;
continue;
}
/**
* @phpstan-ignore property.notFound
* @phpstan-ignore-next-line
*/
$connection->lastMessageTime = $time_now;
continue;
}
/**
* @phpstan-ignore-next-line
*/
if ($time_now - $connection->lastMessageTime > self::HEARTBEAT_TIME) {
$connection->close();
if ($time_now - $connection->lastMessageTime > self::HEARTBEAT_TIME) {
$connection->close();
}
}
}
});
});
}
}

private function onWebSocketConnected(TcpConnection $connection, Request $request): void
Expand Down

0 comments on commit af60d41

Please sign in to comment.