Skip to content

Commit

Permalink
use arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
joedixon committed Nov 22, 2023
1 parent 75e85b0 commit 380dfc4
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 60 deletions.
39 changes: 19 additions & 20 deletions src/Channels/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,29 +82,28 @@ public function subscribed(Connection $connection): bool
/**
* Send a message to all connections subscribed to the channel.
*/
public function broadcast(Application $app, array $payload, Connection $except = null): void
public function broadcast(array $payload, Connection $except = null): void
{
collect($this->connections())
->each(function ($connection) use ($payload, $except) {
if ($except && $except->id() === $connection->connection()->id()) {
return;
}
foreach ($this->connections as $connection) {
if ($except && $except->id() === $connection->connection()->id()) {
return;
}

if (isset($payload['except']) && $payload['except'] === $connection->connection()->id()) {
return;
}
if (isset($payload['except']) && $payload['except'] === $connection->connection()->id()) {
return;
}

try {
$connection->send(
json_encode(
Arr::except($payload, 'except')
)
);
} catch (Exception $e) {
// Output::error('Broadcasting to '.$connection->id().' resulted in an error');
// Output::info($e->getMessage());
}
});
try {
$connection->send(
json_encode(
Arr::except($payload, 'except')
)
);
} catch (Exception $e) {
// Output::error('Broadcasting to '.$connection->id().' resulted in an error');
// Output::info($e->getMessage());
}
}
}

/**
Expand Down
2 changes: 0 additions & 2 deletions src/Channels/PresenceChannel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public function subscribe(Connection $connection, string $auth = null, string $d
parent::subscribe($connection, $auth, $data);

$this->broadcast(
$connection->app(),
[
'event' => 'pusher_internal:member_added',
'data' => $data ? json_decode($data, true) : [],
Expand All @@ -37,7 +36,6 @@ public function unsubscribe(Connection $connection): void

if ($userId = $subscription->data('user_id')) {
$this->broadcast(
$connection->app(),
[
'event' => 'pusher_internal:member_removed',
'data' => ['user_id' => $userId],
Expand Down
5 changes: 2 additions & 3 deletions src/Contracts/ChannelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Laravel\Reverb\Contracts;

use Illuminate\Support\Collection;
use Laravel\Reverb\Application;
use Laravel\Reverb\Channels\Channel;

Expand All @@ -21,7 +20,7 @@ public function for(Application $application): ChannelManager;
/**
* Get all the channels.
*/
public function all(): Collection;
public function all(): array;

/**
* Find the given channel.
Expand All @@ -31,7 +30,7 @@ public function find(string $channel): Channel;
/**
* Get all the connections for the given channels.
*/
public function connections(string $channel = null): Collection;
public function connections(string $channel = null): array;

/**
* Unsubscribe from all channels.
Expand Down
2 changes: 1 addition & 1 deletion src/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public static function dispatchSynchronously(Application $app, array $payload, C
$channel = app(ChannelManager::class)->for($app)->find($channel);
$payload['channel'] = $channel->name();

$channel->broadcast($app, $payload, $connection);
$channel->broadcast($payload, $connection);
}
}
}
13 changes: 6 additions & 7 deletions src/Jobs/PingInactiveConnections.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,13 @@ public function handle(ChannelManager $channels): void
app(ApplicationProvider::class)
->all()
->each(function ($application) use ($channels) {
$channels->for($application)->connections()
->each(function ($connection) {
if ($connection->isActive()) {
return;
}
foreach ($channels->for($application)->connections() as $connection) {
if ($connection->isActive()) {
return;
}

$connection->ping();
});
$connection->ping();
}
});
}
}
35 changes: 16 additions & 19 deletions src/Jobs/PruneStaleConnections.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,22 @@ public function handle(ChannelManager $channels): void
app(ApplicationProvider::class)
->all()
->each(function ($application) use ($channels) {

$channels->for($application)->connections()
->each(function ($connection) {
if (! $connection->isStale()) {
return;
}

$connection->send(json_encode([
'event' => 'pusher:error',
'data' => json_encode([
'code' => 4201,
'message' => 'Pong reply not received in time',
]),
]));

$connection->disconnect();

// Output::info('Connection Pruned', $connection->id());
});
foreach ($channels->for($application)->connections() as $connection) {
if (! $connection->isStale()) {
return;
}

$connection->send(json_encode([
'event' => 'pusher:error',
'data' => json_encode([
'code' => 4201,
'message' => 'Pong reply not received in time',
]),
]));

$connection->disconnect();
// Output::info('Connection Pruned', $connection->id());
}
});
}
}
16 changes: 8 additions & 8 deletions src/Managers/ArrayChannelManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Laravel\Reverb\Managers;

use Illuminate\Support\Collection;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\App;
use Laravel\Reverb\Application;
use Laravel\Reverb\Channels\Channel;
Expand Down Expand Up @@ -41,9 +41,9 @@ public function app(): ?Application
/**
* Get all the channels.
*/
public function all(): Collection
public function all(): array
{
return collect($this->channels());
return $this->channels();
}

/**
Expand All @@ -57,13 +57,13 @@ public function find(string $channel): Channel
/**
* Get all the connections for the given channels.
*/
public function connections(string $channel = null): Collection
public function connections(string $channel = null): array
{
$channels = Collection::wrap($this->channels($channel));
$channels = Arr::wrap($this->channels($channel));

return $channels->reduce(function ($carry, $channel) {
return $carry = $carry->merge($channel->connections());
}, collect());
return array_reduce($channels, function ($carry, $channel) {
return $carry + $channel->connections();
}, []);
}

/**
Expand Down

0 comments on commit 380dfc4

Please sign in to comment.