diff --git a/src/LaravelEcho/Pusher/Channels/Channel.php b/src/LaravelEcho/Pusher/Channels/Channel.php index 3b23406..a54a157 100644 --- a/src/LaravelEcho/Pusher/Channels/Channel.php +++ b/src/LaravelEcho/Pusher/Channels/Channel.php @@ -5,7 +5,7 @@ namespace BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels; use BeyondCode\LaravelWebSockets\Events\ChannelOccupied; use BeyondCode\LaravelWebSockets\Events\ChannelVacated; use BeyondCode\LaravelWebSockets\Events\SubscribedToChannel; -use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\InvalidSignatureException; +use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\InvalidSignature; use Illuminate\Support\Collection; use Ratchet\ConnectionInterface; use stdClass; @@ -39,7 +39,7 @@ class Channel } if (str_after($auth, ':') !== hash_hmac('sha256', $signature, $connection->client->appSecret)) { - throw new InvalidSignatureException(); + throw new InvalidSignature(); } } diff --git a/src/LaravelEcho/Pusher/Exceptions/InvalidConnectionException.php b/src/LaravelEcho/Pusher/Exceptions/InvalidConnection.php similarity index 77% rename from src/LaravelEcho/Pusher/Exceptions/InvalidConnectionException.php rename to src/LaravelEcho/Pusher/Exceptions/InvalidConnection.php index a996bc4..30a465a 100644 --- a/src/LaravelEcho/Pusher/Exceptions/InvalidConnectionException.php +++ b/src/LaravelEcho/Pusher/Exceptions/InvalidConnection.php @@ -2,7 +2,7 @@ namespace BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions; -class InvalidConnectionException extends PusherException +class InvalidConnection extends PusherException { public function __construct() { diff --git a/src/LaravelEcho/Pusher/Exceptions/InvalidSignatureException.php b/src/LaravelEcho/Pusher/Exceptions/InvalidSignature.php similarity index 77% rename from src/LaravelEcho/Pusher/Exceptions/InvalidSignatureException.php rename to src/LaravelEcho/Pusher/Exceptions/InvalidSignature.php index ad71243..94ef279 100644 --- a/src/LaravelEcho/Pusher/Exceptions/InvalidSignatureException.php +++ b/src/LaravelEcho/Pusher/Exceptions/InvalidSignature.php @@ -2,7 +2,7 @@ namespace BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions; -class InvalidSignatureException extends PusherException +class InvalidSignature extends PusherException { public function __construct() { diff --git a/src/LaravelEcho/Pusher/Exceptions/UnknownAppKeyException.php b/src/LaravelEcho/Pusher/Exceptions/UnknownAppKey.php similarity index 81% rename from src/LaravelEcho/Pusher/Exceptions/UnknownAppKeyException.php rename to src/LaravelEcho/Pusher/Exceptions/UnknownAppKey.php index c154d27..76e856f 100644 --- a/src/LaravelEcho/Pusher/Exceptions/UnknownAppKeyException.php +++ b/src/LaravelEcho/Pusher/Exceptions/UnknownAppKey.php @@ -2,7 +2,7 @@ namespace BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions; -class UnknownAppKeyException extends PusherException +class UnknownAppKey extends PusherException { public function __construct(string $appKey) { diff --git a/src/LaravelEcho/WebSocket/Message.php b/src/LaravelEcho/WebSocket/Message.php index 72197eb..d746d6a 100644 --- a/src/LaravelEcho/WebSocket/Message.php +++ b/src/LaravelEcho/WebSocket/Message.php @@ -4,7 +4,6 @@ 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; use stdClass; @@ -30,12 +29,14 @@ class Message implements RespondableMessage public function respond() { - if (starts_with($this->payload->event, 'client-')) { - event(new ClientMessageSent($this->connection, $this->payload)); - - $channel = $this->channelManager->find($this->connection->client->appId, $this->payload->channel); - - optional($channel)->broadcast($this->payload); + if (!starts_with($this->payload->event, 'client-')) { + return; } + + event(new ClientMessageSent($this->connection, $this->payload)); + + $channel = $this->channelManager->find($this->connection->client->appId, $this->payload->channel); + + optional($channel)->broadcast($this->payload); } } \ No newline at end of file diff --git a/src/LaravelEcho/WebSocket/PusherServer.php b/src/LaravelEcho/WebSocket/PusherServer.php index 9a3de9d..aa3e644 100644 --- a/src/LaravelEcho/WebSocket/PusherServer.php +++ b/src/LaravelEcho/WebSocket/PusherServer.php @@ -3,7 +3,6 @@ namespace BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket; use BeyondCode\LaravelWebSockets\Events\ConnectionEstablished; -use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Dashboard; use BeyondCode\LaravelWebSockets\QueryParameters; use Exception; use Ratchet\ConnectionInterface; @@ -12,7 +11,7 @@ use BeyondCode\LaravelWebSockets\WebSocketController; use BeyondCode\LaravelWebSockets\ClientProviders\Client; use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Channels\ChannelManager; use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\PusherException; -use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\UnknownAppKeyException; +use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\UnknownAppKey; class PusherServer extends WebSocketController { @@ -26,11 +25,10 @@ class PusherServer extends WebSocketController function onOpen(ConnectionInterface $connection) { - $this->generateSocketId($connection); - - $this->verifyConnection($connection); - - $this->establishConnection($connection); + $this + ->generateSocketId($connection) + ->verifyConnection($connection) + ->establishConnection($connection); } public function onMessage(ConnectionInterface $connection, MessageInterface $message) @@ -45,7 +43,7 @@ class PusherServer extends WebSocketController $this->channelManager->removeFromAllChannels($connection); } - function onError(ConnectionInterface $connection, Exception $exception) + public function onError(ConnectionInterface $connection, Exception $exception) { if ($exception instanceof PusherException) { $connection->send(json_encode( @@ -54,15 +52,26 @@ class PusherServer extends WebSocketController } } + protected function generateSocketId(ConnectionInterface $connection) + { + $socketId = sprintf("%d.%d", getmypid(), random_int(1, 100000000)); + + $connection->socketId = $socketId; + + return $this; + } + protected function verifyConnection(ConnectionInterface $connection) { $appKey = QueryParameters::create($connection->httpRequest)->get('appKey'); - if (! $client = Client::findByAppKey($appKey)) { - throw new UnknownAppKeyException($appKey); + if (!$client = Client::findByAppKey($appKey)) { + throw new UnknownAppKey($appKey); } $connection->client = $client; + + return $this; } protected function establishConnection(ConnectionInterface $connection) @@ -76,12 +85,7 @@ class PusherServer extends WebSocketController ])); event(new ConnectionEstablished($connection)); - } - protected function generateSocketId(ConnectionInterface $connection) - { - $socketId = sprintf("%d.%d", getmypid(), random_int(1, 100000000)); - - $connection->socketId = $socketId; + return $this; } } \ No newline at end of file diff --git a/tests/ConnectionTest.php b/tests/ConnectionTest.php index a4ad910..ea34ac7 100644 --- a/tests/ConnectionTest.php +++ b/tests/ConnectionTest.php @@ -3,8 +3,8 @@ namespace BeyondCode\LaravelWebSockets\Tests; use BeyondCode\LaravelWebSockets\ClientProviders\Client; -use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\InvalidSignatureException; -use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\UnknownAppKeyException; +use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\InvalidSignature; +use BeyondCode\LaravelWebSockets\LaravelEcho\Pusher\Exceptions\UnknownAppKey; use BeyondCode\LaravelWebSockets\LaravelEcho\WebSocket\PusherServer; use BeyondCode\LaravelWebSockets\Tests\Mocks\Message; @@ -23,7 +23,7 @@ class ConnectionTest extends TestCase /** @test */ public function unknown_app_keys_can_not_connect() { - $this->expectException(UnknownAppKeyException::class); + $this->expectException(UnknownAppKey::class); $this->pusherServer->onOpen($this->getWebSocketConnection('/?appKey=test')); } @@ -90,7 +90,7 @@ class ConnectionTest extends TestCase /** @test */ public function clients_need_valid_auth_signatures_for_private_channels() { - $this->expectException(InvalidSignatureException::class); + $this->expectException(InvalidSignature::class); $connection = $this->getWebSocketConnection(); @@ -134,7 +134,7 @@ class ConnectionTest extends TestCase /** @test */ public function clients_need_valid_auth_signatures_for_presence_channels() { - $this->expectException(InvalidSignatureException::class); + $this->expectException(InvalidSignature::class); $connection = $this->getWebSocketConnection();