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
|
|
|
|
2020-09-12 14:45:07 +00:00
|
|
|
use Ratchet\ConnectionInterface;
|
|
|
|
|
|
2020-09-10 19:59:26 +00:00
|
|
|
class PublicChannelTest extends TestCase
|
|
|
|
|
{
|
|
|
|
|
public function test_connect_to_public_channel()
|
|
|
|
|
{
|
|
|
|
|
$connection = $this->newActiveConnection(['public-channel']);
|
|
|
|
|
|
2020-12-07 21:30:36 +00:00
|
|
|
$this->channelManager
|
|
|
|
|
->getGlobalConnectionsCount('1234', 'public-channel')
|
|
|
|
|
->then(function ($total) {
|
|
|
|
|
$this->assertEquals(1, $total);
|
|
|
|
|
});
|
2020-09-10 19:59:26 +00:00
|
|
|
|
|
|
|
|
$connection->assertSentEvent(
|
2026-04-02 10:44:16 +00:00
|
|
|
'websocket.connection_established',
|
2020-09-10 19:59:26 +00:00
|
|
|
[
|
|
|
|
|
'data' => json_encode([
|
|
|
|
|
'socket_id' => $connection->socketId,
|
|
|
|
|
'activity_timeout' => 30,
|
|
|
|
|
]),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$connection->assertSentEvent(
|
2026-04-02 10:44:16 +00:00
|
|
|
'websocket_internal.subscription_succeeded',
|
2020-09-10 19:59:26 +00:00
|
|
|
['channel' => 'public-channel']
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_unsubscribe_from_public_channel()
|
|
|
|
|
{
|
|
|
|
|
$connection = $this->newActiveConnection(['public-channel']);
|
|
|
|
|
|
2020-12-07 21:30:36 +00:00
|
|
|
$this->channelManager
|
|
|
|
|
->getGlobalConnectionsCount('1234', 'public-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' => 'public-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', 'public-channel')
|
|
|
|
|
->then(function ($total) {
|
|
|
|
|
$this->assertEquals(0, $total);
|
|
|
|
|
});
|
2020-09-10 19:59:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_can_whisper_to_public_channel()
|
|
|
|
|
{
|
|
|
|
|
$this->app['config']->set('websockets.apps.0.enable_client_messages', true);
|
|
|
|
|
|
|
|
|
|
$rick = $this->newActiveConnection(['public-channel']);
|
|
|
|
|
$morty = $this->newActiveConnection(['public-channel']);
|
|
|
|
|
|
|
|
|
|
$message = new Mocks\Message([
|
|
|
|
|
'event' => 'client-test-whisper',
|
|
|
|
|
'data' => [],
|
|
|
|
|
'channel' => 'public-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' => 'public-channel']);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_cannot_whisper_to_public_channel_if_having_whispering_disabled()
|
|
|
|
|
{
|
|
|
|
|
$rick = $this->newActiveConnection(['public-channel']);
|
|
|
|
|
$morty = $this->newActiveConnection(['public-channel']);
|
|
|
|
|
|
|
|
|
|
$message = new Mocks\Message([
|
|
|
|
|
'event' => 'client-test-whisper',
|
|
|
|
|
'data' => [],
|
|
|
|
|
'channel' => 'public-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_public_channels()
|
|
|
|
|
{
|
|
|
|
|
$this->newActiveConnection(['public-channel']);
|
|
|
|
|
$this->newActiveConnection(['public-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(
|
|
|
|
|
ConnectionInterface::class, $connection
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
});
|
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_public_channels()
|
|
|
|
|
{
|
|
|
|
|
$this->runOnlyOnRedisReplication();
|
|
|
|
|
|
|
|
|
|
$connection = $this->newActiveConnection(['public-channel']);
|
|
|
|
|
|
|
|
|
|
$message = new Mocks\Message([
|
|
|
|
|
'appId' => '1234',
|
|
|
|
|
'serverId' => 'different_server_id',
|
|
|
|
|
'event' => 'some-event',
|
|
|
|
|
'data' => [
|
|
|
|
|
'channel' => 'public-channel',
|
|
|
|
|
'test' => 'yes',
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$this->channelManager->onMessage(
|
|
|
|
|
$this->channelManager->getRedisKey('1234', 'public-channel'),
|
|
|
|
|
$message->getPayload()
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// The message does not contain appId and serverId anymore.
|
|
|
|
|
$message = new Mocks\Message([
|
|
|
|
|
'event' => 'some-event',
|
|
|
|
|
'data' => [
|
|
|
|
|
'channel' => 'public-channel',
|
|
|
|
|
'test' => 'yes',
|
|
|
|
|
],
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$connection->assertSentEvent('some-event', $message->getPayloadAsArray());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function test_events_get_replicated_across_connections_for_public_channels()
|
|
|
|
|
{
|
|
|
|
|
$this->runOnlyOnRedisReplication();
|
|
|
|
|
|
|
|
|
|
$connection = $this->newActiveConnection(['public-channel']);
|
|
|
|
|
$receiver = $this->newActiveConnection(['public-channel']);
|
|
|
|
|
|
|
|
|
|
$message = new Mocks\Message([
|
|
|
|
|
'appId' => '1234',
|
|
|
|
|
'serverId' => $this->channelManager->getServerId(),
|
|
|
|
|
'event' => 'some-event',
|
|
|
|
|
'data' => [
|
|
|
|
|
'channel' => 'public-channel',
|
|
|
|
|
'test' => 'yes',
|
|
|
|
|
],
|
|
|
|
|
'socketId' => $connection->socketId,
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
$channel = $this->channelManager->find('1234', 'public-channel');
|
|
|
|
|
|
|
|
|
|
$channel->broadcastToEveryoneExcept(
|
|
|
|
|
$message->getPayloadAsObject(), $connection->socketId, '1234', true
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
$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', 'public-channel'),
|
|
|
|
|
$message->getPayload(),
|
|
|
|
|
]);
|
2020-09-18 09:53:36 +00:00
|
|
|
}
|
2020-09-10 19:59:26 +00:00
|
|
|
}
|