From 2865ada2d91c8caded6d219c58c76197fd108021 Mon Sep 17 00:00:00 2001 From: Joe Dixon Date: Wed, 22 Nov 2023 16:13:54 +0000 Subject: [PATCH] wip --- src/Contracts/Connection.php | 7 +++++-- src/Pusher/Event.php | 32 ++++++++++++++++---------------- src/Server.php | 6 +++--- 3 files changed, 24 insertions(+), 21 deletions(-) diff --git a/src/Contracts/Connection.php b/src/Contracts/Connection.php index ccaa6d80..1083ae44 100644 --- a/src/Contracts/Connection.php +++ b/src/Contracts/Connection.php @@ -21,10 +21,13 @@ abstract class Connection */ protected $hasBeenPinged = false; + protected $pusher; + public function __construct( protected Application $application, protected ?string $origin ) { + $this->pusher = new PusherEvent; } /** @@ -80,7 +83,7 @@ public function ping(): void */ public function touch(): Connection { - // $this->lastSeenAt = (string) now(); + $this->lastSeenAt = (string) Carbon::now(); return $this; } @@ -108,7 +111,7 @@ public function isActive(): bool { return $this->lastSeenAt() && $this->lastSeenAt()->isAfter( - now()->subMinutes( + Carbon::now()->subMinutes( $this->app()->pingInterval() ) ); diff --git a/src/Pusher/Event.php b/src/Pusher/Event.php index bfd813b9..be866cd9 100644 --- a/src/Pusher/Event.php +++ b/src/Pusher/Event.php @@ -12,18 +12,18 @@ class Event /** * Handle a pusher event. */ - public static function handle(Connection $connection, string $event, array $payload = []): void + public function handle(Connection $connection, string $event, array $payload = []): void { match (Str::after($event, 'pusher:')) { - 'connection_established' => self::acknowledge($connection), - 'subscribe' => self::subscribe( + 'connection_established' => $this->acknowledge($connection), + 'subscribe' => $this->subscribe( $connection, $payload['channel'], $payload['auth'] ?? null, $payload['channel_data'] ?? null ), - 'unsubscribe' => self::unsubscribe($connection, $payload['channel']), - 'ping' => self::pong($connection), + 'unsubscribe' => $this->unsubscribe($connection, $payload['channel']), + 'ping' => $this->pong($connection), 'pong' => $connection->touch(), default => throw new Exception('Unknown Pusher event: '.$event), }; @@ -32,9 +32,9 @@ public static function handle(Connection $connection, string $event, array $payl /** * Acknowledge the connection. */ - public static function acknowledge(Connection $connection): void + public function acknowledge(Connection $connection): void { - self::send($connection, 'connection_established', [ + $this->send($connection, 'connection_established', [ 'socket_id' => $connection->id(), 'activity_timeout' => 30, ]); @@ -43,7 +43,7 @@ public static function acknowledge(Connection $connection): void /** * Subscribe to the given channel. */ - public static function subscribe(Connection $connection, string $channel, string $auth = null, string $data = null): void + public function subscribe(Connection $connection, string $channel, string $auth = null, string $data = null): void { $channel = app(ChannelManager::class) ->for($connection->app()) @@ -51,13 +51,13 @@ public static function subscribe(Connection $connection, string $channel, string $channel->subscribe($connection, $auth, $data); - self::sendInternally($connection, 'subscription_succeeded', $channel->name(), $channel->data()); + $this->sendInternally($connection, 'subscription_succeeded', $channel->name(), $channel->data()); } /** * Unsubscribe from the given channel. */ - public static function unsubscribe(Connection $connection, string $channel): void + public function unsubscribe(Connection $connection, string $channel): void { $channel = app(ChannelManager::class) ->for($connection->app()) @@ -68,7 +68,7 @@ public static function unsubscribe(Connection $connection, string $channel): voi /** * Respond to a ping. */ - public static function pong(Connection $connection): void + public function pong(Connection $connection): void { static::send($connection, 'pong'); } @@ -76,7 +76,7 @@ public static function pong(Connection $connection): void /** * Send a ping. */ - public static function ping(Connection $connection): void + public function ping(Connection $connection): void { static::send($connection, 'ping'); } @@ -84,7 +84,7 @@ public static function ping(Connection $connection): void /** * Send a response to the given connection. */ - public static function send(Connection $connection, string $event, array $data = []): void + public function send(Connection $connection, string $event, array $data = []): void { $connection->send( static::formatPayload($event, $data) @@ -94,7 +94,7 @@ public static function send(Connection $connection, string $event, array $data = /** * Send an internal response to the given connection. */ - public static function sendInternally(Connection $connection, string $event, string $channel, array $data = []): void + public function sendInternally(Connection $connection, string $event, string $channel, array $data = []): void { $connection->send( static::formatInternalPayload($event, $data, $channel) @@ -104,7 +104,7 @@ public static function sendInternally(Connection $connection, string $event, str /** * Format the payload for the given event. */ - public static function formatPayload(string $event, array $data = [], string $channel = null, string $prefix = 'pusher:'): string|false + public function formatPayload(string $event, array $data = [], string $channel = null, string $prefix = 'pusher:'): string|false { return json_encode( array_filter([ @@ -118,7 +118,7 @@ public static function formatPayload(string $event, array $data = [], string $ch /** * Format the internal payload for the given event. */ - public static function formatInternalPayload(string $event, array $data = [], $channel = null): string|false + public function formatInternalPayload(string $event, array $data = [], $channel = null): string|false { return static::formatPayload($event, $data, $channel, 'pusher_internal:'); } diff --git a/src/Server.php b/src/Server.php index 68bddc50..57895b9b 100644 --- a/src/Server.php +++ b/src/Server.php @@ -12,7 +12,7 @@ class Server { - public function __construct(protected ChannelManager $channels) + public function __construct(protected ChannelManager $channels, protected PusherEvent $pusher) { // } @@ -27,7 +27,7 @@ public function open(Connection $connection): void $connection->touch(); - PusherEvent::handle($connection, 'pusher:connection_established'); + $this->pusher->handle($connection, 'pusher:connection_established'); // Output::info('Connection Established', $connection->id()); } catch (Exception $e) { @@ -49,7 +49,7 @@ public function message(Connection $from, string $message): void try { match (Str::startsWith($event['event'], 'pusher:')) { - true => PusherEvent::handle( + true => $this->pusher->handle( $from, $event['event'], $event['data'] ?? [],