diff --git a/src/Client.php b/src/Client.php index 3b23419..d35b92d 100644 --- a/src/Client.php +++ b/src/Client.php @@ -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; @@ -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) @@ -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();