diff --git a/app/Client/Client.php b/app/Client/Client.php index a08fcb4..b509911 100644 --- a/app/Client/Client.php +++ b/app/Client/Client.php @@ -15,6 +15,8 @@ use function Ratchet\Client\connect; class Client { + protected const MAX_CONNECTION_RETRIES = 3; + /** @var LoopInterface */ protected $loop; @@ -24,6 +26,9 @@ class Client /** @var CliRequestLogger */ protected $logger; + /** @var int */ + protected $connectionRetries = 0; + /** @var int */ protected $timeConnected = 0; @@ -72,15 +77,17 @@ class Client connect($wsProtocol."://{$this->configuration->host()}:{$this->configuration->port()}/expose/control?authToken={$authToken}", [], [ 'X-Expose-Control' => 'enabled', ], $this->loop) - ->then(function (WebSocket $clientConnection) use ($sharedUrl, $subdomain, $deferred) { + ->then(function (WebSocket $clientConnection) use ($sharedUrl, $subdomain, $deferred, $authToken) { + $this->connectionRetries = 0; + $connection = ControlConnection::create($clientConnection); $connection->authenticate($sharedUrl, $subdomain); - $clientConnection->on('close', function() use ($deferred) { + $clientConnection->on('close', function() use ($deferred, $sharedUrl, $subdomain, $authToken) { $this->logger->error('Connection to server closed.'); - $this->exit($deferred); + $this->retryConnectionOrExit($sharedUrl, $subdomain, $authToken); }); $connection->on('authenticationFailed', function ($data) use ($deferred) { @@ -124,7 +131,11 @@ class Client $deferred->resolve(); }); - }, function (\Exception $e) use ($deferred) { + }, function (\Exception $e) use ($deferred, $sharedUrl, $subdomain, $authToken) { + if ($this->connectionRetries > 0) { + $this->retryConnectionOrExit($sharedUrl, $subdomain, $authToken); + return; + } $this->logger->error("Could not connect to the server."); $this->logger->error($e->getMessage()); @@ -142,4 +153,19 @@ class Client exit(1); }); } + + protected function retryConnectionOrExit(string $sharedUrl, $subdomain, $authToken = '') + { + $this->connectionRetries++; + + if ($this->connectionRetries <= static::MAX_CONNECTION_RETRIES) { + $this->loop->addTimer($this->connectionRetries, function() use ($sharedUrl, $subdomain, $authToken) { + $this->logger->info("Retrying connection ({$this->connectionRetries}/".static::MAX_CONNECTION_RETRIES.")"); + + $this->connectToServer($sharedUrl, $subdomain, $authToken); + }); + } else { + exit(1); + } + } } diff --git a/config/expose.php b/config/expose.php index dd59497..9d34956 100644 --- a/config/expose.php +++ b/config/expose.php @@ -1,8 +1,8 @@ 'localhost', - 'port' => 8080, + 'host' => 'expose.dev', + 'port' => 443, 'auth_token' => '', 'admin' => [ diff --git a/resources/views/client/dashboard.twig b/resources/views/client/dashboard.twig index f49b405..0566a4a 100644 --- a/resources/views/client/dashboard.twig +++ b/resources/views/client/dashboard.twig @@ -100,8 +100,8 @@ -
- Status code: @{ currentLog.response?.status} + + Received at: @{ currentLog.performed_at } + + @{ currentLog.response?.status } - @{ currentLog.response?.reason } + + + @{ currentLog.response?.status } - @{ currentLog.response?.reason } + + + @{ currentLog.response?.status } - @{ currentLog.response?.reason } +