diff --git a/src/Events/Concerns/Dispatchable.php b/src/Events/Concerns/Dispatchable.php new file mode 100644 index 00000000..a9336a8b --- /dev/null +++ b/src/Events/Concerns/Dispatchable.php @@ -0,0 +1,22 @@ +getMessage()); + } + } +} diff --git a/src/Events/ConnectionClosed.php b/src/Events/ConnectionClosed.php new file mode 100644 index 00000000..1bf4365c --- /dev/null +++ b/src/Events/ConnectionClosed.php @@ -0,0 +1,19 @@ +connections->add($connection, $data ? json_decode($data, true) : []); + + SubscribedToChannel::dispatch($connection, $this->name(), $auth); } /** @@ -78,6 +82,8 @@ public function unsubscribe(Connection $connection): void if ($this->connections->isEmpty()) { app(ChannelManager::class)->for($connection->app())->remove($this); } + + UnsubscribedFromChannel::dispatch($connection, $this->name()); } /** diff --git a/src/Protocols/Pusher/Server.php b/src/Protocols/Pusher/Server.php index c7caf71c..f8b83fca 100644 --- a/src/Protocols/Pusher/Server.php +++ b/src/Protocols/Pusher/Server.php @@ -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; @@ -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); } @@ -82,6 +87,8 @@ public function close(Connection $connection): void $connection->disconnect(); Log::info('Connection Closed', $connection->id()); + + ConnectionClosed::dispatch($connection); } /** @@ -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); } /** diff --git a/src/Servers/Reverb/Console/Commands/StartServer.php b/src/Servers/Reverb/Console/Commands/StartServer.php index 79807bfc..f23920fe 100644 --- a/src/Servers/Reverb/Console/Commands/StartServer.php +++ b/src/Servers/Reverb/Console/Commands/StartServer.php @@ -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; @@ -68,6 +70,8 @@ public function handle(): void $this->components->info('Starting '.($server->isSecure() ? 'secure ' : '')."server on {$host}:{$port}".(($hostname && $hostname !== $host) ? " ({$hostname})" : '')); $server->start(); + + ServerStarted::dispatch(); } /** @@ -108,6 +112,8 @@ protected function ensureRestartCommandIsRespected(Server $server, LoopInterface $server->stop(); + ServerStopped::dispatch(); + $this->components->info("Stopping server on {$host}:{$port}"); }); }