2020-09-10 19:59:26 +00:00
|
|
|
<?php
|
|
|
|
|
|
2025-01-16 07:54:02 +00:00
|
|
|
namespace BlaxSoftware\LaravelWebSockets\Test;
|
2020-09-10 19:59:26 +00:00
|
|
|
|
2025-01-16 07:54:02 +00:00
|
|
|
use BlaxSoftware\LaravelWebSockets\Server\Exceptions\InvalidSignature;
|
2020-09-12 14:45:07 +00:00
|
|
|
use Ratchet\ConnectionInterface;
|
2020-09-10 19:59:26 +00:00
|
|
|
|
|
|
|
|
class PrivateChannelTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
public function test_connect_to_private_channel_with_invalid_signature()
|
|
|
|
|
{
|
|
|
|
|
$connection = $this->newConnection();
|
|
|
|
|
|
|
|
|
|
$message = new Mocks\Message([
|
2026-04-02 10:44:16 +00:00
|
|
|
'event' => 'websocket.subscribe',
|
2020-09-10 19:59:26 +00:00
|
|
|
'data' => [
|
|
|
|
|
'auth' => 'invalid',
|
|
|
|
|
'channel' => 'private-channel',
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
2026-04-02 10:44:16 +00:00
|
|
|
$this->wsHandler->onOpen($connection);
|
|
|
|
|
$this->wsHandler->onMessage($connection, $message);
|
|
|
|
|
|
BF reject private/presence subscribe with invalid signature
handleSubscription() recorded the socket in $channel_connections and in the
buffered ws_channel_connections_* / ws_active_channels cache before
$channel->subscribe() ran, and swallowed the InvalidSignature it threw. On
the same frame shouldRejectMessage() found the entry and called
saveConnection(), and the client got "websocket.subscribe:response Success".
One subscribe with a bad signature put the socket on the private or presence
channel, where it received broadcasts, and WebsocketService listed it.
- Track the socket only after subscribe() succeeds (or it is already on the
channel).
- A failed private/presence subscribe answers "<event>:error" with the
exception's message and code (Invalid Signature, 4009), drops any earlier
tracking of the socket on that channel, takes it off the channel if the
subscribe had already saved it, and ends the frame before
shouldRejectMessage() and the Success reply.
- Public channels behave as before.
Tests assert, after the single subscribe frame, the error reply, no channel
membership, no $channel_connections entry and no cache listing, and that a
later message on the channel does not resubscribe the socket.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 10:56:38 +00:00
|
|
|
// One frame: refused with an error, not on the channel, not tracked.
|
|
|
|
|
$this->assertSubscriptionRefused($connection, 'private-channel');
|
|
|
|
|
$this->assertNotContains(
|
|
|
|
|
'private-channel',
|
|
|
|
|
\BlaxSoftware\LaravelWebSockets\Services\WebsocketService::getActiveChannels()
|
|
|
|
|
);
|
2020-09-10 19:59:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_connect_to_private_channel_with_valid_signature()
|
|
|
|
|
{
|
|
|
|
|
$connection = $this->newConnection();
|
|
|
|
|
|
2026-04-02 10:44:16 +00:00
|
|
|
$this->wsHandler->onOpen($connection);
|
2020-09-10 19:59:26 +00:00
|
|
|
|
2020-09-18 09:15:49 +00:00
|
|
|
$message = new Mocks\SignedMessage([
|
2026-04-02 10:44:16 +00:00
|
|
|
'event' => 'websocket.subscribe',
|
2020-09-10 19:59:26 +00:00
|
|
|
'data' => [
|
|
|
|
|
'channel' => 'private-channel',
|
|
|
|
|
],
|
2020-09-18 09:15:49 +00:00
|
|
|
], $connection, 'private-channel');
|
2020-09-10 19:59:26 +00:00
|
|
|
|
2026-04-02 10:44:16 +00:00
|
|
|
$this->wsHandler->onMessage($connection, $message);
|
2020-09-10 19:59:26 +00:00
|
|
|
|
2026-04-02 10:44:16 +00:00
|
|
|
$connection->assertSentEvent('websocket_internal.subscription_succeeded', [
|
2020-09-10 19:59:26 +00:00
|
|
|
'channel' => 'private-channel',
|
|
|
|
|
]);
|
|
|
|
|
|
2020-12-07 21:30:36 +00:00
|
|
|
$this->channelManager
|
|
|
|
|
->getGlobalConnectionsCount('1234', 'private-channel')
|
|
|
|
|
->then(function ($total) {
|
|
|
|
|
$this->assertEquals(1, $total);
|
|
|
|
|
});
|
2020-09-10 19:59:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_unsubscribe_from_private_channel()
|
|
|
|
|
{
|
|
|
|
|
$connection = $this->newPrivateConnection('private-channel');
|
|
|
|
|
|
2020-12-07 21:30:36 +00:00
|
|
|
$this->channelManager
|
|
|
|
|
->getGlobalConnectionsCount('1234', 'private-channel')
|
|
|
|
|
->then(function ($total) {
|
|
|
|
|
$this->assertEquals(1, $total);
|
|
|
|
|
});
|
2020-09-10 19:59:26 +00:00
|
|
|
|
|
|
|
|
$message = new Mocks\Message([
|
2026-04-02 10:44:16 +00:00
|
|
|
'event' => 'websocket.unsubscribe',
|
2020-09-10 19:59:26 +00:00
|
|
|
'data' => [
|
|
|
|
|
'channel' => 'private-channel',
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
2026-04-02 10:44:16 +00:00
|
|
|
$this->wsHandler->onMessage($connection, $message);
|
2020-09-10 19:59:26 +00:00
|
|
|
|
2020-12-07 21:30:36 +00:00
|
|
|
$this->channelManager
|
|
|
|
|
->getGlobalConnectionsCount('1234', 'private-channel')
|
|
|
|
|
->then(function ($total) {
|
|
|
|
|
$this->assertEquals(0, $total);
|
|
|
|
|
});
|
2020-09-10 19:59:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_can_whisper_to_private_channel()
|
|
|
|
|
{
|
|
|
|
|
$this->app['config']->set('websockets.apps.0.enable_client_messages', true);
|
|
|
|
|
|
|
|
|
|
$rick = $this->newPrivateConnection('private-channel');
|
|
|
|
|
$morty = $this->newPrivateConnection('private-channel');
|
|
|
|
|
|
|
|
|
|
$message = new Mocks\Message([
|
|
|
|
|
'event' => 'client-test-whisper',
|
|
|
|
|
'data' => [],
|
|
|
|
|
'channel' => 'private-channel',
|
|
|
|
|
]);
|
|
|
|
|
|
2026-04-02 10:44:16 +00:00
|
|
|
$this->wsHandler->onMessage($rick, $message);
|
2020-09-10 19:59:26 +00:00
|
|
|
|
|
|
|
|
$rick->assertNotSentEvent('client-test-whisper');
|
|
|
|
|
$morty->assertSentEvent('client-test-whisper', ['data' => [], 'channel' => 'private-channel']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_cannot_whisper_to_public_channel_if_having_whispering_disabled()
|
|
|
|
|
{
|
|
|
|
|
$rick = $this->newPrivateConnection('private-channel');
|
|
|
|
|
$morty = $this->newPrivateConnection('private-channel');
|
|
|
|
|
|
|
|
|
|
$message = new Mocks\Message([
|
|
|
|
|
'event' => 'client-test-whisper',
|
|
|
|
|
'data' => [],
|
|
|
|
|
'channel' => 'private-channel',
|
|
|
|
|
]);
|
|
|
|
|
|
2026-04-02 10:44:16 +00:00
|
|
|
$this->wsHandler->onMessage($rick, $message);
|
2020-09-10 19:59:26 +00:00
|
|
|
|
|
|
|
|
$rick->assertNotSentEvent('client-test-whisper');
|
|
|
|
|
$morty->assertNotSentEvent('client-test-whisper');
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-12 14:45:07 +00:00
|
|
|
public function test_local_connections_for_private_channels()
|
|
|
|
|
{
|
|
|
|
|
$this->newPrivateConnection('private-channel');
|
|
|
|
|
$this->newPrivateConnection('private-channel-2');
|
|
|
|
|
|
2020-12-07 21:30:36 +00:00
|
|
|
$this->channelManager
|
|
|
|
|
->getLocalConnections()
|
|
|
|
|
->then(function ($connections) {
|
|
|
|
|
$this->assertCount(2, $connections);
|
|
|
|
|
|
|
|
|
|
foreach ($connections as $connection) {
|
|
|
|
|
$this->assertInstanceOf(
|
2026-04-02 10:44:31 +00:00
|
|
|
ConnectionInterface::class,
|
|
|
|
|
$connection
|
2020-12-07 21:30:36 +00:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-09-12 14:45:07 +00:00
|
|
|
}
|
2020-09-18 09:53:36 +00:00
|
|
|
|
|
|
|
|
public function test_events_are_processed_by_on_message_on_private_channels()
|
|
|
|
|
{
|
|
|
|
|
$this->runOnlyOnRedisReplication();
|
|
|
|
|
|
|
|
|
|
$connection = $this->newPrivateConnection('private-channel');
|
|
|
|
|
|
|
|
|
|
$message = new Mocks\SignedMessage([
|
|
|
|
|
'appId' => '1234',
|
|
|
|
|
'serverId' => 'different_server_id',
|
|
|
|
|
'event' => 'some-event',
|
|
|
|
|
'data' => [
|
|
|
|
|
'channel' => 'private-channel',
|
|
|
|
|
'test' => 'yes',
|
|
|
|
|
],
|
|
|
|
|
], $connection, 'private-channel');
|
|
|
|
|
|
|
|
|
|
$this->channelManager->onMessage(
|
|
|
|
|
$this->channelManager->getRedisKey('1234', 'private-channel'),
|
|
|
|
|
$message->getPayload()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// The message does not contain appId and serverId anymore.
|
|
|
|
|
$message = new Mocks\SignedMessage([
|
|
|
|
|
'event' => 'some-event',
|
|
|
|
|
'data' => [
|
|
|
|
|
'channel' => 'private-channel',
|
|
|
|
|
'test' => 'yes',
|
|
|
|
|
],
|
|
|
|
|
], $connection, 'private-channel');
|
|
|
|
|
|
|
|
|
|
$connection->assertSentEvent('some-event', $message->getPayloadAsArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_events_get_replicated_across_connections_for_private_channels()
|
|
|
|
|
{
|
|
|
|
|
$this->runOnlyOnRedisReplication();
|
|
|
|
|
|
|
|
|
|
$connection = $this->newPrivateConnection('private-channel');
|
|
|
|
|
$receiver = $this->newPrivateConnection('private-channel');
|
|
|
|
|
|
|
|
|
|
$message = new Mocks\SignedMessage([
|
|
|
|
|
'appId' => '1234',
|
|
|
|
|
'serverId' => $this->channelManager->getServerId(),
|
|
|
|
|
'event' => 'some-event',
|
|
|
|
|
'data' => [
|
|
|
|
|
'channel' => 'private-channel',
|
|
|
|
|
'test' => 'yes',
|
|
|
|
|
],
|
|
|
|
|
'socketId' => $connection->socketId,
|
|
|
|
|
], $connection, 'private-channel');
|
|
|
|
|
|
|
|
|
|
$channel = $this->channelManager->find('1234', 'private-channel');
|
|
|
|
|
|
|
|
|
|
$channel->broadcastToEveryoneExcept(
|
2026-04-02 10:44:31 +00:00
|
|
|
$message->getPayloadAsObject(),
|
|
|
|
|
$connection->socketId,
|
|
|
|
|
'1234',
|
|
|
|
|
true
|
2020-09-18 09:53:36 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$receiver->assertSentEvent('some-event', $message->getPayloadAsArray());
|
|
|
|
|
|
2020-12-07 21:30:12 +00:00
|
|
|
$this->getSubscribeClient()
|
|
|
|
|
->assertNothingDispatched();
|
2020-09-18 09:53:36 +00:00
|
|
|
|
2020-12-07 21:30:36 +00:00
|
|
|
$this->getPublishClient()
|
|
|
|
|
->assertCalledWithArgs('publish', [
|
|
|
|
|
$this->channelManager->getRedisKey('1234', 'private-channel'),
|
|
|
|
|
$message->getPayload(),
|
|
|
|
|
]);
|
2020-09-18 09:53:36 +00:00
|
|
|
}
|
2020-09-10 19:59:26 +00:00
|
|
|
}
|