Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
joedixon committed Nov 22, 2023
1 parent 633161c commit 2865ada
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
7 changes: 5 additions & 2 deletions src/Contracts/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -80,7 +83,7 @@ public function ping(): void
*/
public function touch(): Connection
{
// $this->lastSeenAt = (string) now();
$this->lastSeenAt = (string) Carbon::now();

return $this;
}
Expand Down Expand Up @@ -108,7 +111,7 @@ public function isActive(): bool
{
return $this->lastSeenAt() &&
$this->lastSeenAt()->isAfter(
now()->subMinutes(
Carbon::now()->subMinutes(
$this->app()->pingInterval()
)
);
Expand Down
32 changes: 16 additions & 16 deletions src/Pusher/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -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),
};
Expand All @@ -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,
]);
Expand All @@ -43,21 +43,21 @@ 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())
->find($channel);

$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())
Expand All @@ -68,23 +68,23 @@ 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');
}

/**
* Send a ping.
*/
public static function ping(Connection $connection): void
public function ping(Connection $connection): void
{
static::send($connection, 'ping');
}

/**
* 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)
Expand All @@ -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)
Expand All @@ -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([
Expand All @@ -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:');
}
Expand Down
6 changes: 3 additions & 3 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

class Server
{
public function __construct(protected ChannelManager $channels)
public function __construct(protected ChannelManager $channels, protected PusherEvent $pusher)
{
//
}
Expand All @@ -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) {
Expand All @@ -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'] ?? [],
Expand Down

0 comments on commit 2865ada

Please sign in to comment.