From 02d788ac7840b8ff386435896d4bb45cfdf88ece Mon Sep 17 00:00:00 2001 From: freek Date: Wed, 21 Nov 2018 23:47:46 +0100 Subject: [PATCH] wip --- src/Console/StartWebSocketServer.php | 2 + src/LaravelEcho/Pusher/PusherMessage.php | 71 +++++++++++++++++++ src/LaravelEcho/WebSocket/EchoServer.php | 57 ++------------- src/LaravelEcho/WebSocket/Message.php | 39 ++++++++++ .../WebSocket/RespondableMessage.php | 7 ++ .../WebSocket/RespondableMessageFactory.php | 24 +++++++ 6 files changed, 149 insertions(+), 51 deletions(-) create mode 100644 src/LaravelEcho/Pusher/PusherMessage.php create mode 100644 src/LaravelEcho/WebSocket/Message.php create mode 100644 src/LaravelEcho/WebSocket/RespondableMessage.php create mode 100644 src/LaravelEcho/WebSocket/RespondableMessageFactory.php diff --git a/src/Console/StartWebSocketServer.php b/src/Console/StartWebSocketServer.php index 996f32a..3aef3ae 100644 --- a/src/Console/StartWebSocketServer.php +++ b/src/Console/StartWebSocketServer.php @@ -41,6 +41,8 @@ class StartWebSocketServer extends Command // TODO: add an option to not start the echo server WebSocketRouter::echo(); + // TODO: add flag for verbose mode, to send more things to console + (new WebsocketServer($this->option('port'), '0.0.0.0', $loop))->run(); } } \ No newline at end of file diff --git a/src/LaravelEcho/Pusher/PusherMessage.php b/src/LaravelEcho/Pusher/PusherMessage.php new file mode 100644 index 0000000..4a1f413 --- /dev/null +++ b/src/LaravelEcho/Pusher/PusherMessage.php @@ -0,0 +1,71 @@ +payload = $payload; + + $this->connection = $connection; + + $this->channelManager = $channelManager; + } + + public function respond() + { + $eventName = camel_case(str_after($this->payload->event, ':')); + + if (method_exists($this, $eventName)) { + call_user_func([$this, $eventName], $this->connection, $this->payload->data); + } + } + + /** + * @link https://pusher.com/docs/pusher_protocol#ping-pong + * + * @param ConnectionInterface $connection + * @param $payload + */ + protected function ping(ConnectionInterface $connection, $payload) + { + $connection->send(json_encode([ + 'event' => 'pusher:pong', + ])); + } + + /** + * @link https://pusher.com/docs/pusher_protocol#pusher-subscribe + * + * @param ConnectionInterface $conn + * @param $payload + */ + protected function subscribe(ConnectionInterface $connection, $payload) + { + $channel = $this->channelManager->findOrCreate($connection->appId, $payload->channel); + + $channel->subscribe($connection, $payload); + } + + public function unsubscribe(ConnectionInterface $connection, stdClass $payload) + { + $channel = $this->channelManager->findOrCreate($connection->appId, $payload->channel); + + $channel->unsubscribe($connection); + } +} diff --git a/src/LaravelEcho/WebSocket/EchoServer.php b/src/LaravelEcho/WebSocket/EchoServer.php index 34bc53b..df7d38d 100644 --- a/src/LaravelEcho/WebSocket/EchoServer.php +++ b/src/LaravelEcho/WebSocket/EchoServer.php @@ -2,6 +2,7 @@ namespace BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket; +use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\PusherMessage; use Ratchet\ConnectionInterface; use Ratchet\RFC6455\Messaging\MessageInterface; use BeyondCode\LaravelWebSockets\WebSocketController; @@ -20,12 +21,13 @@ class EchoServer extends WebSocketController /** * When a new connection is opened it will be passed to this method + * * @param ConnectionInterface $conn The socket/connection that just connected to your application + * * @throws \Exception */ function onOpen(ConnectionInterface $conn) { - dump("Client connected"); /** * There are a couple things we need to do here: @@ -51,29 +53,12 @@ class EchoServer extends WebSocketController ])); } - public function onMessage(ConnectionInterface $conn, MessageInterface $msg) + public function onMessage(ConnectionInterface $conn, MessageInterface $message) { - $payload = json_decode($msg->getPayload()); + $message = RespondableMessageFactory::createForMessage($message, $conn, $this->channelManager); - dump("Received payload", $payload); + $message->respond(); - /** - * Pusher events get a special treatment - */ - if (starts_with($payload->event, 'pusher:')) { - $event = camel_case(str_replace(':', '_', $payload->event)); - - if (method_exists($this, $event)) { - call_user_func([$this, $event], $conn, $payload->data); - } - } else { - // Try to find a channel and broadcast the message to the clients. - $channel = $this->channelManager->find($conn->appId, $payload->channel); - - if ($channel) { - $channel->broadcast($payload); - } - } } public function onClose(ConnectionInterface $connection) @@ -81,36 +66,6 @@ class EchoServer extends WebSocketController $this->channelManager->removeFromAllChannels($connection); } - /** - * @link https://pusher.com/docs/pusher_protocol#ping-pong - * @param ConnectionInterface $conn - * @param $payload - */ - protected function pusherPing(ConnectionInterface $conn, $payload) - { - $conn->send($this->buildPayload('pusher:pong')); - } - - /** - * @link https://pusher.com/docs/pusher_protocol#pusher-subscribe - * @param ConnectionInterface $conn - * @param $payload - */ - protected function pusherSubscribe(ConnectionInterface $conn, $payload) - { - $channel = $this->channelManager->findOrCreate($conn->appId, $payload->channel); - - $channel->subscribe($conn, $payload); - } - - public function pusherUnsubscribe(ConnectionInterface $connection, stdClass $payload) - { - $channel = $this->channelManager->findOrCreate($connection->appId, $payload->channel); - - $channel->unsubscribe($connection); - - } - protected function buildPayload($event, $data = []) { return json_encode([ diff --git a/src/LaravelEcho/WebSocket/Message.php b/src/LaravelEcho/WebSocket/Message.php new file mode 100644 index 0000000..321d602 --- /dev/null +++ b/src/LaravelEcho/WebSocket/Message.php @@ -0,0 +1,39 @@ +payload = $payload; + + $this->connection = $connection; + + $this->channelManager = $channelManager; + } + + public function respond() + { + $channel = $this->channelManager->find($this->connection->appId, $this->payload->channel); + + if (!$channel) { + return; + } + + $channel->broadcast($this->payload); + } +} \ No newline at end of file diff --git a/src/LaravelEcho/WebSocket/RespondableMessage.php b/src/LaravelEcho/WebSocket/RespondableMessage.php new file mode 100644 index 0000000..6b77181 --- /dev/null +++ b/src/LaravelEcho/WebSocket/RespondableMessage.php @@ -0,0 +1,7 @@ +getPayload()); + + return starts_with($payload->event, 'pusher:') + ? new PusherMessage($payload, $connection, $channelManager) + : new Message($payload, $connection, $channelManager); + } +} \ No newline at end of file