Skip to content

Commit

Permalink
Merge remote-tracking branch 'michaelnabil230/events' into events
Browse files Browse the repository at this point in the history
  • Loading branch information
d0m4te committed Jul 12, 2024
2 parents 8383876 + ef09146 commit fb2df43
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 14 deletions.
22 changes: 22 additions & 0 deletions src/Events/Concerns/Dispatchable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Laravel\Reverb\Events\Concerns;

use Laravel\Reverb\Loggers\Log;

trait Dispatchable
{
/**
* Dispatch the event with the given arguments.
*
* @return mixed
*/
public static function dispatch()
{
try {
return event(new static(...func_get_args()));
} catch (\Exception $exception) {
Log::error($exception->getMessage());
}
}
}
19 changes: 19 additions & 0 deletions src/Events/ConnectionClosed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Laravel\Reverb\Events;

use Laravel\Reverb\Contracts\Connection;
use Laravel\Reverb\Events\Concerns\Dispatchable;

class ConnectionClosed
{
use Dispatchable;

/**
* Create a new event instance.
*/
public function __construct(public Connection $connection)
{
//
}
}
19 changes: 19 additions & 0 deletions src/Events/ConnectionError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Laravel\Reverb\Events;

use Laravel\Reverb\Contracts\Connection;
use Laravel\Reverb\Events\Concerns\Dispatchable;

class ConnectionError
{
use Dispatchable;

/**
* Create a new event instance.
*/
public function __construct(public Connection $connection, public \Exception $exception)
{
//
}
}
2 changes: 1 addition & 1 deletion src/Events/MessageReceived.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Laravel\Reverb\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Laravel\Reverb\Contracts\Connection;
use Laravel\Reverb\Events\Concerns\Dispatchable;

class MessageReceived
{
Expand Down
2 changes: 1 addition & 1 deletion src/Events/MessageSent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

namespace Laravel\Reverb\Events;

use Illuminate\Foundation\Events\Dispatchable;
use Laravel\Reverb\Contracts\Connection;
use Laravel\Reverb\Events\Concerns\Dispatchable;

class MessageSent
{
Expand Down
19 changes: 19 additions & 0 deletions src/Events/NewConnection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Laravel\Reverb\Events;

use Laravel\Reverb\Contracts\Connection;
use Laravel\Reverb\Events\Concerns\Dispatchable;

class NewConnection
{
use Dispatchable;

/**
* Create a new event instance.
*/
public function __construct(public Connection $connection)
{
//
}
}
18 changes: 18 additions & 0 deletions src/Events/ServerStarted.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Laravel\Reverb\Events;

use Laravel\Reverb\Events\Concerns\Dispatchable;

class ServerStarted
{
use Dispatchable;

/**
* Create a new event instance.
*/
public function __construct()
{
//
}
}
18 changes: 18 additions & 0 deletions src/Events/ServerStopped.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Laravel\Reverb\Events;

use Laravel\Reverb\Events\Concerns\Dispatchable;

class ServerStopped
{
use Dispatchable;

/**
* Create a new event instance.
*/
public function __construct()
{
//
}
}
22 changes: 22 additions & 0 deletions src/Events/SubscribedToChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Laravel\Reverb\Events;

use Laravel\Reverb\Contracts\Connection;
use Laravel\Reverb\Events\Concerns\Dispatchable;

class SubscribedToChannel
{
use Dispatchable;

/**
* Create a new event instance.
*/
public function __construct(
public Connection $connection,
public string $channelName,
public ?string $auth = null,
) {
//
}
}
21 changes: 21 additions & 0 deletions src/Events/UnsubscribedFromChannel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Laravel\Reverb\Events;

use Laravel\Reverb\Contracts\Connection;
use Laravel\Reverb\Events\Concerns\Dispatchable;

class UnsubscribedFromChannel
{
use Dispatchable;

/**
* Create a new event instance.
*/
public function __construct(
public Connection $connection,
public string $channelName,
) {
//
}
}
6 changes: 6 additions & 0 deletions src/Protocols/Pusher/Channels/Channel.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

use Laravel\Reverb\Contracts\Connection;
use Laravel\Reverb\Loggers\Log;
use Laravel\Reverb\Events\SubscribedToChannel;
use Laravel\Reverb\Events\UnsubscribedFromChannel;
use Laravel\Reverb\Protocols\Pusher\Concerns\SerializesChannels;
use Laravel\Reverb\Protocols\Pusher\Contracts\ChannelConnectionManager;
use Laravel\Reverb\Protocols\Pusher\Contracts\ChannelManager;
Expand Down Expand Up @@ -67,6 +69,8 @@ public function findById(string $id): ?Connection
public function subscribe(Connection $connection, ?string $auth = null, ?string $data = null): void
{
$this->connections->add($connection, $data ? json_decode($data, associative: true, flags: JSON_THROW_ON_ERROR) : []);

SubscribedToChannel::dispatch($connection, $this->name(), $auth);
}

/**
Expand All @@ -79,6 +83,8 @@ public function unsubscribe(Connection $connection): void
if ($this->connections->isEmpty()) {
app(ChannelManager::class)->for($connection->app())->remove($this);
}

UnsubscribedFromChannel::dispatch($connection, $this->name());
}

/**
Expand Down
31 changes: 19 additions & 12 deletions src/Protocols/Pusher/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
use Exception;
use Illuminate\Support\Str;
use Laravel\Reverb\Contracts\Connection;
use Laravel\Reverb\Events\ConnectionClosed;
use Laravel\Reverb\Events\ConnectionError;
use Laravel\Reverb\Events\MessageReceived;
use Laravel\Reverb\Events\NewConnection;
use Laravel\Reverb\Loggers\Log;
use Laravel\Reverb\Protocols\Pusher\Contracts\ChannelManager;
use Laravel\Reverb\Protocols\Pusher\Exceptions\InvalidOrigin;
Expand Down Expand Up @@ -34,6 +37,8 @@ public function open(Connection $connection): void
$this->handler->handle($connection, 'pusher:connection_established');

Log::info('Connection Established', $connection->id());

NewConnection::dispatch($connection);
} catch (Exception $e) {
$this->error($connection, $e);
}
Expand Down Expand Up @@ -82,6 +87,8 @@ public function close(Connection $connection): void
$connection->disconnect();

Log::info('Connection Closed', $connection->id());

ConnectionClosed::dispatch($connection);
}

/**
Expand All @@ -93,21 +100,21 @@ public function error(Connection $connection, Exception $exception): void
$connection->send(json_encode($exception->payload()));

Log::error('Message from '.$connection->id().' resulted in a pusher error');
Log::info($exception->getMessage());

return;
} else {
$connection->send(json_encode([
'event' => 'pusher:error',
'data' => json_encode([
'code' => 4200,
'message' => 'Invalid message format',
]),
]));

Log::error('Message from '.$connection->id().' resulted in an unknown error');
}

$connection->send(json_encode([
'event' => 'pusher:error',
'data' => json_encode([
'code' => 4200,
'message' => 'Invalid message format',
]),
]));

Log::error('Message from '.$connection->id().' resulted in an unknown error');
Log::info($exception->getMessage());

ConnectionError::dispatch($connection, $exception);
}

/**
Expand Down
6 changes: 6 additions & 0 deletions src/Servers/Reverb/Console/Commands/StartServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Laravel\Reverb\Application;
use Laravel\Reverb\Contracts\ApplicationProvider;
use Laravel\Reverb\Contracts\Logger;
use Laravel\Reverb\Events\ServerStarted;
use Laravel\Reverb\Events\ServerStopped;
use Laravel\Reverb\Jobs\PingInactiveConnections;
use Laravel\Reverb\Jobs\PruneStaleConnections;
use Laravel\Reverb\Loggers\CliLogger;
Expand Down Expand Up @@ -72,6 +74,8 @@ public function handle(): void
$this->components->info('Starting '.($server->isSecure() ? 'secure ' : '')."server on {$host}:{$port}".(($hostname && $hostname !== $host) ? " ({$hostname})" : ''));

$server->start();

ServerStarted::dispatch();
}

/**
Expand Down Expand Up @@ -112,6 +116,8 @@ protected function ensureRestartCommandIsRespected(Server $server, LoopInterface

$server->stop();

ServerStopped::dispatch();

$this->components->info("Stopping server on {$host}:{$port}");
});
}
Expand Down

0 comments on commit fb2df43

Please sign in to comment.