Avoid calls to app()
This commit is contained in:
parent
00e8f3e1a8
commit
990a075b20
|
|
@ -5,13 +5,23 @@ namespace BeyondCode\LaravelWebSockets\HttpApi\Controllers;
|
|||
use Illuminate\Support\Str;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Collection;
|
||||
use React\Promise\PromiseInterface;
|
||||
use Symfony\Component\HttpKernel\Exception\HttpException;
|
||||
use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface;
|
||||
use BeyondCode\LaravelWebSockets\WebSockets\Channels\ChannelManager;
|
||||
use BeyondCode\LaravelWebSockets\WebSockets\Channels\PresenceChannel;
|
||||
|
||||
class FetchChannelsController extends Controller
|
||||
{
|
||||
/** @var ReplicationInterface */
|
||||
protected $replication;
|
||||
|
||||
public function __construct(ChannelManager $channelManager, ReplicationInterface $replication)
|
||||
{
|
||||
parent::__construct($channelManager);
|
||||
|
||||
$this->replication = $replication;
|
||||
}
|
||||
|
||||
public function __invoke(Request $request)
|
||||
{
|
||||
$attributes = [];
|
||||
|
|
@ -39,10 +49,8 @@ class FetchChannelsController extends Controller
|
|||
return $channel->getChannelName();
|
||||
})->toArray();
|
||||
|
||||
/** @var PromiseInterface $memberCounts */
|
||||
// We ask the replication backend to get us the member count per channel
|
||||
$memberCounts = app(ReplicationInterface::class)
|
||||
->channelMemberCounts($request->appId, $channelNames);
|
||||
$memberCounts = $this->replication->channelMemberCounts($request->appId, $channelNames);
|
||||
|
||||
// We return a promise since the backend runs async. We get $counts back
|
||||
// as a key-value array of channel names and their member count.
|
||||
|
|
|
|||
|
|
@ -15,7 +15,7 @@ class Channel
|
|||
protected $channelName;
|
||||
|
||||
/** @var ReplicationInterface */
|
||||
protected $pubSub;
|
||||
protected $replication;
|
||||
|
||||
/** @var \Ratchet\ConnectionInterface[] */
|
||||
protected $subscribedConnections = [];
|
||||
|
|
@ -23,7 +23,7 @@ class Channel
|
|||
public function __construct(string $channelName)
|
||||
{
|
||||
$this->channelName = $channelName;
|
||||
$this->pubSub = app(ReplicationInterface::class);
|
||||
$this->replication = app(ReplicationInterface::class);
|
||||
}
|
||||
|
||||
public function getChannelName(): string
|
||||
|
|
@ -68,7 +68,7 @@ class Channel
|
|||
$this->saveConnection($connection);
|
||||
|
||||
// Subscribe to broadcasted messages from the pub/sub backend
|
||||
$this->pubSub->subscribe($connection->app->id, $this->channelName);
|
||||
$this->replication->subscribe($connection->app->id, $this->channelName);
|
||||
|
||||
$connection->send(json_encode([
|
||||
'event' => 'pusher_internal:subscription_succeeded',
|
||||
|
|
@ -81,7 +81,7 @@ class Channel
|
|||
unset($this->subscribedConnections[$connection->socketId]);
|
||||
|
||||
// Unsubscribe from the pub/sub backend
|
||||
$this->pubSub->unsubscribe($connection->app->id, $this->channelName);
|
||||
$this->replication->unsubscribe($connection->app->id, $this->channelName);
|
||||
|
||||
if (! $this->hasConnections()) {
|
||||
DashboardLogger::vacated($connection, $this->channelName);
|
||||
|
|
@ -111,7 +111,7 @@ class Channel
|
|||
public function broadcastToOthers(ConnectionInterface $connection, $payload)
|
||||
{
|
||||
// Also broadcast via the other websocket servers
|
||||
$this->pubSub->publish($connection->app->id, $this->channelName, $payload);
|
||||
$this->replication->publish($connection->app->id, $this->channelName, $payload);
|
||||
|
||||
$this->broadcastToEveryoneExcept($payload, $connection->socketId);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,6 @@ namespace BeyondCode\LaravelWebSockets\WebSockets\Channels;
|
|||
use stdClass;
|
||||
use Ratchet\ConnectionInterface;
|
||||
use React\Promise\PromiseInterface;
|
||||
use BeyondCode\LaravelWebSockets\PubSub\ReplicationInterface;
|
||||
use BeyondCode\LaravelWebSockets\WebSockets\Exceptions\InvalidSignature;
|
||||
|
||||
class PresenceChannel extends Channel
|
||||
|
|
@ -24,12 +23,12 @@ class PresenceChannel extends Channel
|
|||
|
||||
/**
|
||||
* @param string $appId
|
||||
* @return array|PromiseInterface
|
||||
* @return PromiseInterface
|
||||
*/
|
||||
public function getUsers(string $appId)
|
||||
{
|
||||
// Get the members list from the replication backend
|
||||
return app(ReplicationInterface::class)
|
||||
return $this->replication
|
||||
->channelMembers($appId, $this->channelName);
|
||||
}
|
||||
|
||||
|
|
@ -50,7 +49,7 @@ class PresenceChannel extends Channel
|
|||
$this->users[$connection->socketId] = $channelData;
|
||||
|
||||
// Add the connection as a member of the channel
|
||||
app(ReplicationInterface::class)
|
||||
$this->replication
|
||||
->joinChannel(
|
||||
$connection->app->id,
|
||||
$this->channelName,
|
||||
|
|
@ -60,7 +59,7 @@ class PresenceChannel extends Channel
|
|||
|
||||
// We need to pull the channel data from the replication backend,
|
||||
// otherwise we won't be sending the full details of the channel
|
||||
app(ReplicationInterface::class)
|
||||
$this->replication
|
||||
->channelMembers($connection->app->id, $this->channelName)
|
||||
->then(function ($users) use ($connection) {
|
||||
// Send the success event
|
||||
|
|
@ -87,7 +86,7 @@ class PresenceChannel extends Channel
|
|||
}
|
||||
|
||||
// Remove the connection as a member of the channel
|
||||
app(ReplicationInterface::class)
|
||||
$this->replication
|
||||
->leaveChannel(
|
||||
$connection->app->id,
|
||||
$this->channelName,
|
||||
|
|
@ -107,11 +106,11 @@ class PresenceChannel extends Channel
|
|||
|
||||
/**
|
||||
* @param string|null $appId
|
||||
* @return PromiseInterface|array
|
||||
* @return PromiseInterface
|
||||
*/
|
||||
public function toArray(string $appId = null)
|
||||
{
|
||||
return app(ReplicationInterface::class)
|
||||
return $this->replication
|
||||
->channelMembers($appId, $this->channelName)
|
||||
->then(function ($users) {
|
||||
return array_merge(parent::toArray(), [
|
||||
|
|
|
|||
Loading…
Reference in New Issue