This commit is contained in:
Marcel Pociot 2018-11-22 22:20:23 +01:00
parent ef0d45fb6f
commit b698b95147
3 changed files with 48 additions and 1 deletions

View File

@ -0,0 +1,42 @@
<?php
namespace BeyondCode\LaravelWebSockets\LaravelEcho\Http\Controllers;
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager;
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\PresenceChannel;
use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\InvalidSignatureException;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Symfony\Component\HttpKernel\Exception\HttpException;
class FetchChannels extends EchoController
{
/** @var \BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager */
protected $channelManager;
public function __construct(ChannelManager $channelManager)
{
$this->channelManager = $channelManager;
}
public function __invoke(Request $request)
{
$channels = Collection::make($this->channelManager->getChannels($request->appId))->filter(function ($channel) {
return $channel instanceof PresenceChannel;
});
if ($request->has('filter_by_prefix')) {
$channels = $channels->filter(function ($channel, $channelName) use ($request) {
return starts_with($channelName, $request->filter_by_prefix);
});
}
return [
'channels' => $channels->map(function ($channel) {
return [
'user_count' => count($channel->getUsers()),
];
})->toArray()
];
}
}

View File

@ -42,6 +42,11 @@ class ChannelManager
return Channel::class;
}
public function getChannels(string $appId): array
{
return $this->channels[$appId] ?? [];
}
public function removeFromAllChannels(ConnectionInterface $connection)
{
collect($this->channels[$connection->appId])->each->unsubscribe($connection);

View File

@ -68,7 +68,7 @@ class Router
//TODO: add origin checker middleware
$this->get('/app/{appKey}', LaravelEcho\WebSocket\PusherServer::class);
$this->get('/apps/{appId}/channels', LaravelEcho\Http\Controllers\StatusController::class);
$this->get('/apps/{appId}/channels', LaravelEcho\Http\Controllers\FetchChannels::class);
$this->get('/apps/{appId}/channels/{channelName}', LaravelEcho\Http\Controllers\FetchChannel::class);
$this->get('/apps/{appId}/channels/{channelName}/users', LaravelEcho\Http\Controllers\FetchUsers::class);