diff --git a/composer.json b/composer.json index 38b4c71..4ebd5c7 100644 --- a/composer.json +++ b/composer.json @@ -28,8 +28,10 @@ "illuminate/console": "5.7.*", "illuminate/http": "5.7.*", "illuminate/routing": "5.7.*", + "illuminate/broadcasting": "5.7.*", "illuminate/support": "5.7.*", "symfony/http-kernel": "~4.0", + "pusher/pusher-php-server": "~3.0", "symfony/psr-http-message-bridge": "^1.1" }, "require-dev": { diff --git a/resources/views/console.blade.php b/resources/views/console.blade.php index e3366f4..1fc817f 100644 --- a/resources/views/console.blade.php +++ b/resources/views/console.blade.php @@ -7,31 +7,53 @@ src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"> + -
-
-
-
- WebSockets Console +
+
+
-
- -
- -
-
- + + + + + +
+
-
-

Events

+
+
+

Event Creator

+
+
+
+ +
+
+ +
+
+
+
+
+ +
+
+
+
+
+ +
+
+
+
+

Events

@@ -42,38 +64,113 @@ + + + + + +
@{{ log.type }}@{{ log.socketId }}@{{ log.details }}@{{ log.time }}
\ No newline at end of file diff --git a/src/Http/Controllers/SendMessage.php b/src/Http/Controllers/SendMessage.php new file mode 100644 index 0000000..ff9b39b --- /dev/null +++ b/src/Http/Controllers/SendMessage.php @@ -0,0 +1,21 @@ +key, $request->secret, + $request->appId, config('broadcasting.connections.pusher.options', []) + ); + + return (new PusherBroadcaster($pusher)) + ->broadcast([$request->channel], $request->event, json_decode($request->data, true)); + } +} \ No newline at end of file diff --git a/src/Http/routes.php b/src/Http/routes.php index 82d1f1a..b41fb75 100644 --- a/src/Http/routes.php +++ b/src/Http/routes.php @@ -1,4 +1,5 @@ verifySignature($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([ diff --git a/src/LaravelEcho/Pusher/Channels/Channel.php b/src/LaravelEcho/Pusher/Channels/Channel.php index d84ea03..0eb9864 100644 --- a/src/LaravelEcho/Pusher/Channels/Channel.php +++ b/src/LaravelEcho/Pusher/Channels/Channel.php @@ -2,6 +2,7 @@ namespace BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels; +use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Dashboard; use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\InvalidSignatureException; use Illuminate\Support\Collection; use Ratchet\ConnectionInterface; @@ -56,11 +57,21 @@ class Channel public function unsubscribe(ConnectionInterface $connection) { unset($this->subscriptions[$connection->socketId]); + + if (! $this->hasConnections()) { + Dashboard::vacated($connection, $this->channelId); + } } protected function saveConnection(ConnectionInterface $connection) { + if (! $this->hasConnections()) { + Dashboard::occupied($connection, $this->channelId); + } + $this->subscriptions[$connection->socketId] = $connection; + + Dashboard::subscribed($connection, $this->channelId); } public function broadcast($payload) diff --git a/src/LaravelEcho/Pusher/Dashboard.php b/src/LaravelEcho/Pusher/Dashboard.php new file mode 100644 index 0000000..8a325e0 --- /dev/null +++ b/src/LaravelEcho/Pusher/Dashboard.php @@ -0,0 +1,100 @@ +httpRequest; + + self::log($connection->client->appId, self::TYPE_CONNECTION, [ + 'details' => "Origin: {$request->getUri()->getScheme()}://{$request->getUri()->getHost()}", + 'socketId' => $connection->socketId, + ]); + } + + public static function disconnection(ConnectionInterface $connection) + { + self::log($connection->client->appId, self::TYPE_DISCONNECTION, [ + 'socketId' => $connection->socketId + ]); + } + + public static function vacated(ConnectionInterface $connection, string $channelId) + { + self::log($connection->client->appId, self::TYPE_VACATED, [ + 'details' => "Channel: {$channelId}" + ]); + } + + public static function occupied(ConnectionInterface $connection, string $channelId) + { + self::log($connection->client->appId, self::TYPE_OCCUPIED, [ + 'details' => "Channel: {$channelId}" + ]); + } + + public static function subscribed(ConnectionInterface $connection, string $channelId) + { + self::log($connection->client->appId, self::TYPE_SUBSCRIBED, [ + 'socketId' => $connection->socketId, + 'details' => "Channel: {$channelId}" + ]); + } + + public static function clientMessage(ConnectionInterface $connection, stdClass $payload) + { + self::log($connection->client->appId, self::TYPE_CLIENT_MESSAGE, [ + 'details' => "Channel: {$payload->channel}, Event: {$payload->event}", + 'socketId' => $connection->socketId, + 'data' => json_encode($payload) + ]); + } + + public static function apiMessage($appId, string $channel, string $event, string $payload) + { + self::log($appId, self::TYPE_API_MESSAGE, [ + 'details' => "Channel: {$channel}, Event: {$event}", + 'data' => $payload + ]); + } + + public static function log($appId, string $type, array $attributes = []) + { + $channelId = self::LOG_CHANNEL_PREFIX . $type; + + $channel = app(ChannelManager::class)->find($appId, $channelId); + + optional($channel)->broadcast([ + 'event' => 'log_message', + 'channel' => $channelId, + 'data' => [ + 'type' => $type, + 'time' => strftime("%H:%M:%S") + ] + $attributes + ]); + } + +} \ No newline at end of file diff --git a/src/LaravelEcho/WebSocket/Message.php b/src/LaravelEcho/WebSocket/Message.php index ad089d2..5617f59 100644 --- a/src/LaravelEcho/WebSocket/Message.php +++ b/src/LaravelEcho/WebSocket/Message.php @@ -3,6 +3,7 @@ namespace BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket; use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager; +use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Dashboard; use Ratchet\ConnectionInterface; use stdClass; @@ -29,6 +30,8 @@ class Message implements RespondableMessage public function respond() { if (starts_with($this->payload->event, 'client-')) { + Dashboard::clientMessage($this->connection, $this->payload); + $channel = $this->channelManager->find($this->connection->client->appId, $this->payload->channel); optional($channel)->broadcast($this->payload); diff --git a/src/LaravelEcho/WebSocket/PusherServer.php b/src/LaravelEcho/WebSocket/PusherServer.php index b1024c1..b81127e 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\LaravelEcho\Pusher\Dashboard; use Exception; use Ratchet\ConnectionInterface; use Ratchet\RFC6455\Messaging\MessageInterface; @@ -68,6 +69,8 @@ class PusherServer extends WebSocketController protected function establishConnection(ConnectionInterface $connection) { + Dashboard::connection($connection); + $connection->send(json_encode([ 'event' => 'pusher:connection_established', 'data' => json_encode([ @@ -83,20 +86,4 @@ class PusherServer extends WebSocketController $connection->socketId = $socketId; } - - public function log(string $appId, string $type, string $details) - { - $channelId = "private-logger-{$type}"; - - $channel = $this->channelManager->find($appId, $channelId); - - optional($channel)->broadcast([ - 'event' => $type, - 'channel' => $channelId, - 'data' => [ - 'type' => $type, - 'details' => $details - ] - ]); - } } \ No newline at end of file