Skip to content

Commit

Permalink
Test ping timer
Browse files Browse the repository at this point in the history
  • Loading branch information
lotharthesavior committed Dec 28, 2023
1 parent 79acf09 commit 761d48c
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Kanata\ConveyorServerClient;

use co;
use Exception;
use OpenSwoole\Timer;
use Psr\Log\LoggerInterface;
use WebSocket\BadOpcodeException;
use WebSocket\Client as WsClient;
Expand Down Expand Up @@ -79,6 +81,11 @@ class Client implements ClientInterface
*/
protected int $reconnectionAttemptsCount = 0;

/**
* @var ?int Timer id for ping.
*/
protected ?int $pingTimer = null;

protected ?LoggerInterface $logger = null;

public function __construct(array $options)
Expand All @@ -95,7 +102,26 @@ public function getClient(): ?WsClient
return $this->client;
}

public function getPingTimer(): ?int
{
return $this->pingTimer;
}

/**
* @throws TimeoutException|Exception
*/
public function connect(): void
{
co::run(function () {
go(fn () => $this->startConnection());
go(fn () => Timer::tick(5000, function (int $timerId) {
$this->pingTimer = $timerId;
$this->getClient()->ping();
}));
});
}

private function startConnection()
{
try {
$this->handleClientConnection();
Expand Down

0 comments on commit 761d48c

Please sign in to comment.