123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- <?php
-
- namespace Illuminate\Broadcasting\Broadcasters;
-
- use Illuminate\Support\Arr;
- use Illuminate\Support\Str;
- use Illuminate\Contracts\Redis\Factory as Redis;
- use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
-
- class RedisBroadcaster extends Broadcaster
- {
- /**
- * The Redis instance.
- *
- * @var \Illuminate\Contracts\Redis\Factory
- */
- protected $redis;
-
- /**
- * The Redis connection to use for broadcasting.
- *
- * @var string
- */
- protected $connection;
-
- /**
- * Create a new broadcaster instance.
- *
- * @param \Illuminate\Contracts\Redis\Factory $redis
- * @param string|null $connection
- * @return void
- */
- public function __construct(Redis $redis, $connection = null)
- {
- $this->redis = $redis;
- $this->connection = $connection;
- }
-
- /**
- * Authenticate the incoming request for a given channel.
- *
- * @param \Illuminate\Http\Request $request
- * @return mixed
- * @throws \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
- */
- public function auth($request)
- {
- if (Str::startsWith($request->channel_name, ['private-', 'presence-']) &&
- ! $request->user()) {
- throw new AccessDeniedHttpException;
- }
-
- $channelName = Str::startsWith($request->channel_name, 'private-')
- ? Str::replaceFirst('private-', '', $request->channel_name)
- : Str::replaceFirst('presence-', '', $request->channel_name);
-
- return parent::verifyUserCanAccessChannel(
- $request, $channelName
- );
- }
-
- /**
- * Return the valid authentication response.
- *
- * @param \Illuminate\Http\Request $request
- * @param mixed $result
- * @return mixed
- */
- public function validAuthenticationResponse($request, $result)
- {
- if (is_bool($result)) {
- return json_encode($result);
- }
-
- return json_encode(['channel_data' => [
- 'user_id' => $request->user()->getAuthIdentifier(),
- 'user_info' => $result,
- ]]);
- }
-
- /**
- * Broadcast the given event.
- *
- * @param array $channels
- * @param string $event
- * @param array $payload
- * @return void
- */
- public function broadcast(array $channels, $event, array $payload = [])
- {
- $connection = $this->redis->connection($this->connection);
-
- $payload = json_encode([
- 'event' => $event,
- 'data' => $payload,
- 'socket' => Arr::pull($payload, 'socket'),
- ]);
-
- foreach ($this->formatChannels($channels) as $channel) {
- $connection->publish($channel, $payload);
- }
- }
- }
|