Merge pull request #524 from beyondcode/feature/healthchecks

[feature] Healthcheck endpoint
This commit is contained in:
rennokki 2020-09-13 08:17:10 +00:00 committed by GitHub
commit 1dcf8538ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 90 additions and 0 deletions

View File

@ -267,6 +267,8 @@ return [
'websocket' => \BeyondCode\LaravelWebSockets\Server\WebSocketHandler::class, 'websocket' => \BeyondCode\LaravelWebSockets\Server\WebSocketHandler::class,
'health' => \BeyondCode\LaravelWebSockets\Server\HealthHandler::class,
'trigger_event' => \BeyondCode\LaravelWebSockets\API\TriggerEvent::class, 'trigger_event' => \BeyondCode\LaravelWebSockets\API\TriggerEvent::class,
'fetch_channels' => \BeyondCode\LaravelWebSockets\API\FetchChannels::class, 'fetch_channels' => \BeyondCode\LaravelWebSockets\API\FetchChannels::class,

View File

@ -0,0 +1,65 @@
<?php
namespace BeyondCode\LaravelWebSockets\Server;
use Exception;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\RequestInterface;
use Ratchet\ConnectionInterface;
use Ratchet\RFC6455\Messaging\MessageInterface;
use Ratchet\WebSocket\MessageComponentInterface;
class HealthHandler implements MessageComponentInterface
{
/**
* Handle the socket opening.
*
* @param \Ratchet\ConnectionInterface $connection
* @return void
*/
public function onOpen(ConnectionInterface $connection, RequestInterface $request = null)
{
$response = new Response(
200,
['Content-Type' => 'application/json'],
json_encode(['ok' => true])
);
tap($connection)->send(\GuzzleHttp\Psr7\str($response))->close();
}
/**
* Handle the incoming message.
*
* @param \Ratchet\ConnectionInterface $connection
* @param \Ratchet\RFC6455\Messaging\MessageInterface $message
* @return void
*/
public function onMessage(ConnectionInterface $connection, MessageInterface $message)
{
//
}
/**
* Handle the websocket close.
*
* @param \Ratchet\ConnectionInterface $connection
* @return void
*/
public function onClose(ConnectionInterface $connection)
{
//
}
/**
* Handle the websocket errors.
*
* @param \Ratchet\ConnectionInterface $connection
* @param WebSocketException $exception
* @return void
*/
public function onError(ConnectionInterface $connection, Exception $exception)
{
//
}
}

View File

@ -49,6 +49,7 @@ class Router
$this->get('/apps/{appId}/channels', config('websockets.handlers.fetch_channels')); $this->get('/apps/{appId}/channels', config('websockets.handlers.fetch_channels'));
$this->get('/apps/{appId}/channels/{channelName}', config('websockets.handlers.fetch_channel')); $this->get('/apps/{appId}/channels/{channelName}', config('websockets.handlers.fetch_channel'));
$this->get('/apps/{appId}/channels/{channelName}/users', config('websockets.handlers.fetch_users')); $this->get('/apps/{appId}/channels/{channelName}/users', config('websockets.handlers.fetch_users'));
$this->get('/health', config('websockets.handlers.health'));
} }
/** /**

22
tests/HealthTest.php Normal file
View File

@ -0,0 +1,22 @@
<?php
namespace BeyondCode\LaravelWebSockets\Test;
use BeyondCode\LaravelWebSockets\Server\HealthHandler;
use Illuminate\Support\Str;
class HealthTest extends TestCase
{
public function test_health_handler()
{
$connection = $this->newConnection();
$this->pusherServer = app(HealthHandler::class);
$this->pusherServer->onOpen($connection);
$this->assertTrue(
Str::contains($connection->sentRawData[0], '{"ok":true}')
);
}
}