Skip to content

Commit

Permalink
reimplement stats
Browse files Browse the repository at this point in the history
  • Loading branch information
joedixon committed Nov 5, 2023
1 parent eede814 commit e8f04f5
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
44 changes: 44 additions & 0 deletions src/Http/Controllers/StatsController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Laravel\Reverb\Http\Controllers;

use Illuminate\Support\Facades\App;
use Laravel\Reverb\Contracts\ChannelManager;
use Laravel\Reverb\Contracts\ConnectionManager;
use Psr\Http\Message\RequestInterface;
use Ratchet\ConnectionInterface;
use Ratchet\Http\HttpServerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;

class StatsController implements HttpServerInterface
{
public function onOpen(ConnectionInterface $conn, RequestInterface $request = null)
{
tap($conn)->send(new JsonResponse((object) [
'connections' => App::make(ConnectionManager::class)->all()->count(),
'channels' => App::make(ChannelManager::class)->all()->map(function ($channel) {
return [
'name' => $channel->name(),
'connections' => App::make(ChannelManager::class)
->connectionKeys($channel)
->count(),
];
}),
]))->close();
}

public function onMessage(ConnectionInterface $from, $message)
{
//
}

public function onClose(ConnectionInterface $connection)
{
//
}

public function onError(ConnectionInterface $connection, \Exception $e)
{
//
}
}
2 changes: 2 additions & 0 deletions src/Servers/Ratchet/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Facades\App;
use Laravel\Reverb\Http\Controllers\EventController;
use Laravel\Reverb\Http\Controllers\StatsController;
use Ratchet\Http\HttpServer;
use Ratchet\Http\Router;
use Ratchet\Server\IoServer;
Expand Down Expand Up @@ -46,6 +47,7 @@ protected static function routes(): RouteCollection
$routes = new RouteCollection();
$routes->add('sockets', new Route('/app/{appId}', ['_controller' => static::handler()], [], [], null, [], ['GET']));
$routes->add('events', new Route('/apps/{appId}/events', ['_controller' => EventController::class], [], [], null, [], ['POST']));
$routes->add('stats', new Route('/stats', ['_controller' => StatsController::class], [], [], null, [], ['GET']));

return $routes;
}
Expand Down

0 comments on commit e8f04f5

Please sign in to comment.