Skip to content

Commit

Permalink
simplify channel connections
Browse files Browse the repository at this point in the history
  • Loading branch information
joedixon committed Nov 29, 2023
1 parent 1ef63c0 commit ae6e442
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 23 deletions.
29 changes: 8 additions & 21 deletions src/Channels/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,17 @@

namespace Laravel\Reverb\Channels;

use Illuminate\Support\Arr;
use Laravel\Reverb\Contracts\ChannelConnectionManager;
use Laravel\Reverb\Contracts\Connection;

class Channel
{
/**
* The channel connections.
*
* @var \Laravel\Reverb\Contracts\ChannelConnectionManager
*/
protected $connections;
protected $connections = [];

public function __construct(protected string $name)
{
$this->connections = app(ChannelConnectionManager::class);
}

/**
Expand All @@ -28,52 +23,44 @@ public function name(): string
return $this->name;
}

/**
* Get all connections for the channel.
*/
public function connections(): array
{
return $this->connections->all();
}

/**
* Find a connection.
*/
public function find(Connection $connection): ?Connection
{
return $this->connections->find($connection);
return $this->connections[$connection->id()] ?? null;
}

/**
* Find a connection by its ID.
*/
public function findById(string $id): ?Connection
{
return $this->connections->findById($id);
return $this->connections[$id] ?? null;
}

/**
* Subscribe to the given channel.
*/
public function subscribe(Connection $connection, string $auth = null, string $data = null): void
{
$this->connections->add($connection, $data ? json_decode($data, true) : []);
$this->connections[$connection->id()] = $connection;
}

/**
* Unsubscribe from the given channel.
*/
public function unsubscribe(Connection $connection): void
{
$this->connections->remove($connection);
unset($this->connections[$connection->id()]);
}

/**
* Determine if the connection is subscribed to the channel.
*/
public function subscribed(Connection $connection): bool
{
return $this->connections->find($connection) !== null;
return $this->find($connection) !== null;
}

/**
Expand All @@ -89,7 +76,7 @@ public function broadcast(array $payload, Connection $except = null): void

$message = json_encode($payload);

foreach ($this->connections() as $connection) {
foreach ($this->connections as $connection) {
if ($except->id() === $connection->id()) {
continue;
}
Expand All @@ -105,7 +92,7 @@ public function broadcastToAll(array $payload): void
{
$message = json_encode($payload);

foreach ($this->connections() as $connection) {
foreach ($this->connections as $connection) {
$connection->send($message);
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/WebSockets/WsConnection.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@

namespace Laravel\Reverb\WebSockets;

use Evenement\EventEmitter;
use Laravel\Reverb\Http\Connection;
use Ratchet\RFC6455\Messaging\CloseFrameChecker;
use Ratchet\RFC6455\Messaging\DataInterface;
use Ratchet\RFC6455\Messaging\Frame;
use Ratchet\RFC6455\Messaging\FrameInterface;
use Ratchet\RFC6455\Messaging\MessageBuffer;

class WsConnection extends EventEmitter
class WsConnection
{
/**
* The message buffer.
Expand Down

0 comments on commit ae6e442

Please sign in to comment.