diff --git a/src/Dashboard/EventHandler.php b/src/Dashboard/EventHandler.php deleted file mode 100644 index 460b2b6..0000000 --- a/src/Dashboard/EventHandler.php +++ /dev/null @@ -1,8 +0,0 @@ -appId, + $event->channeldId, + $event->name, + $event->data + ); + } + + public function onChannelVacated(ChannelVacated $event) + { + Dashboard::vacated($event->connection, $event->channelId); + } + + public function onClientMessageSent(ClientMessageSent $event) + { + Dashboard::clientMessage($event->connection, $event->payload); + } + + public function onConnectionEstablished(ConnectionEstablished $event) + { + Dashboard::connection($event->connection); + } + + public function subscribe(Dispatcher $events) + { + $events->listen(ApiMessageSent::class, static::class. '@onApiMessageSent'); + $events->listen(ChannelVacated::class, static::class . '@onChannelVacated'); + $events->listen(ClientMessageSent::class, static::class . '@onClientMessageSent'); + } +} \ No newline at end of file diff --git a/src/Events/ApiMessageSent.php b/src/Events/ApiMessageSent.php new file mode 100644 index 0000000..68f77d6 --- /dev/null +++ b/src/Events/ApiMessageSent.php @@ -0,0 +1,25 @@ +appId = $appId; + + $this->channeldId = $channeldId; + + $this->name = $name; + + $this->data = $data; + } +} \ No newline at end of file diff --git a/src/Events/ClientMessageSent.php b/src/Events/ClientMessageSent.php new file mode 100644 index 0000000..7c171ed --- /dev/null +++ b/src/Events/ClientMessageSent.php @@ -0,0 +1,22 @@ +connection = $connection; + + $this->payload = $payload; + } +} \ No newline at end of file diff --git a/src/Events/ConnectionEstablished.php b/src/Events/ConnectionEstablished.php new file mode 100644 index 0000000..296505b --- /dev/null +++ b/src/Events/ConnectionEstablished.php @@ -0,0 +1,17 @@ +connection = $connection; + } +} \ No newline at end of file diff --git a/src/LaravelEcho/Http/Controllers/TriggerEvent.php b/src/LaravelEcho/Http/Controllers/TriggerEvent.php index 7630ee1..9e0cf59 100644 --- a/src/LaravelEcho/Http/Controllers/TriggerEvent.php +++ b/src/LaravelEcho/Http/Controllers/TriggerEvent.php @@ -2,7 +2,7 @@ namespace BeyondCode\LaravelWebSockets\LaravelEcho\Http\Controllers; -use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Dashboard; +use BeyondCode\LaravelWebSockets\Events\ApiMessageSent; use Illuminate\Http\Request; class TriggerEvent extends EchoController @@ -12,13 +12,6 @@ class TriggerEvent extends EchoController $this->ensureValidSignature($request); foreach ($request->json()->get('channels', []) as $channelId) { - Dashboard::apiMessage( - $request->appId, - $channelId, - $request->json()->get('name'), - $request->json()->get('data') - ); - $channel = $this->channelManager->find($request->appId, $channelId); optional($channel)->broadcastToEveryoneExcept([ @@ -26,6 +19,13 @@ class TriggerEvent extends EchoController 'event' => $request->json()->get('name'), 'data' => $request->json()->get('data'), ], $request->json()->get('socket_id')); + + event(new ApiMessageSent( + $request->appId, + $channelId, + $request->json()->get('name'), + $request->json()->get('data') + )); } return $request->json()->all(); diff --git a/src/LaravelEcho/Pusher/Channels/Channel.php b/src/LaravelEcho/Pusher/Channels/Channel.php index 759c83c..d7a1b3e 100644 --- a/src/LaravelEcho/Pusher/Channels/Channel.php +++ b/src/LaravelEcho/Pusher/Channels/Channel.php @@ -61,7 +61,6 @@ class Channel if (! $this->hasConnections()) { event(new ChannelVacated($connection, $this->channelId)); - Dashboard::vacated($connection, $this->channelId); } } diff --git a/src/LaravelEcho/WebSocket/Message.php b/src/LaravelEcho/WebSocket/Message.php index 5617f59..72197eb 100644 --- a/src/LaravelEcho/WebSocket/Message.php +++ b/src/LaravelEcho/WebSocket/Message.php @@ -2,6 +2,7 @@ namespace BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket; +use BeyondCode\LaravelWebSockets\Events\ClientMessageSent; use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager; use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Dashboard; use Ratchet\ConnectionInterface; @@ -30,7 +31,7 @@ class Message implements RespondableMessage public function respond() { if (starts_with($this->payload->event, 'client-')) { - Dashboard::clientMessage($this->connection, $this->payload); + event(new ClientMessageSent($this->connection, $this->payload)); $channel = $this->channelManager->find($this->connection->client->appId, $this->payload->channel); diff --git a/src/LaravelEcho/WebSocket/PusherServer.php b/src/LaravelEcho/WebSocket/PusherServer.php index 5ea0806..9a3de9d 100644 --- a/src/LaravelEcho/WebSocket/PusherServer.php +++ b/src/LaravelEcho/WebSocket/PusherServer.php @@ -2,6 +2,7 @@ namespace BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket; +use BeyondCode\LaravelWebSockets\Events\ConnectionEstablished; use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Dashboard; use BeyondCode\LaravelWebSockets\QueryParameters; use Exception; @@ -66,8 +67,6 @@ class PusherServer extends WebSocketController protected function establishConnection(ConnectionInterface $connection) { - Dashboard::connection($connection); - $connection->send(json_encode([ 'event' => 'pusher:connection_established', 'data' => json_encode([ @@ -75,6 +74,8 @@ class PusherServer extends WebSocketController 'activity_timeout' => 60, ]) ])); + + event(new ConnectionEstablished($connection)); } protected function generateSocketId(ConnectionInterface $connection) diff --git a/src/WebSocketsServiceProvider.php b/src/WebSocketsServiceProvider.php index 49a185e..5f1168f 100644 --- a/src/WebSocketsServiceProvider.php +++ b/src/WebSocketsServiceProvider.php @@ -2,6 +2,8 @@ namespace BeyondCode\LaravelWebSockets; +use BeyondCode\LaravelWebSockets\Dashboard\EventSubscriber; +use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Gate; use Illuminate\Support\Facades\Route; use BeyondCode\LaravelWebSockets\ClientProviders\ClientProvider; @@ -27,6 +29,8 @@ class WebSocketsServiceProvider extends ServiceProvider $this->commands([ Console\StartWebSocketServer::class, ]); + + Event::subscribe(EventSubscriber::class); } protected function registerRoutes()