diff --git a/src/ApiSettings.php b/src/ApiSettings.php index e1eea20..2164fb8 100644 --- a/src/ApiSettings.php +++ b/src/ApiSettings.php @@ -33,7 +33,7 @@ public static function getVersion(string $version = ''): string * @param string $appId * @return string */ - public static function createUrl(string $appId): string + public static function createUrl(string $appId, string $cluster = null): string { $query = [ 'client' => 'api-clients/pusher (https://php-api-clients.org/clients/pusher)', @@ -41,7 +41,9 @@ public static function createUrl(string $appId): string 'version' => ApiSettings::getVersion(), ]; - return 'wss://ws.pusherapp.com/app/' . + $host = ($cluster) ? "ws-{$cluster}.pusher.com" : 'ws.pusherapp.com'; + + return 'wss://'.$host.'/app/' . $appId . '?' . http_build_query($query) ; diff --git a/src/AsyncClient.php b/src/AsyncClient.php index d165400..3a1c23e 100644 --- a/src/AsyncClient.php +++ b/src/AsyncClient.php @@ -83,7 +83,7 @@ public function __construct(Subject $client) * @throws \InvalidArgumentException * @return AsyncClient */ - public static function create(LoopInterface $loop, string $app, Resolver $resolver = null): AsyncClient + public static function create(LoopInterface $loop, string $app, Resolver $resolver = null, $cluster = null): AsyncClient { try { Scheduler::setDefaultFactory(function () use ($loop) { @@ -93,7 +93,7 @@ public static function create(LoopInterface $loop, string $app, Resolver $resolv } return new self( - WebSocket::createFactory(ApiSettings::createUrl($app), false, [], $loop, $resolver) + WebSocket::createFactory(ApiSettings::createUrl($app, $cluster), false, [], $loop, $resolver) ); } diff --git a/tests/ApiSettingsTest.php b/tests/ApiSettingsTest.php index 411fe77..b45bbf6 100644 --- a/tests/ApiSettingsTest.php +++ b/tests/ApiSettingsTest.php @@ -43,5 +43,9 @@ public function testCreateUrl() $expectedUrl = 'wss://ws.pusherapp.com/app/barBaz?client=api-clients%2Fpusher+%28https%3A%2F%2Fphp-api-clients.org%2Fclients%2Fpusher%29&protocol=7&version=' . ApiSettings::getVersion(); self::assertSame($expectedUrl, ApiSettings::createUrl('barBaz')); + + $expectedUrl = 'wss://ws-ap1.pusher.com/app/barBaz?client=api-clients%2Fpusher+%28https%3A%2F%2Fphp-api-clients.org%2Fclients%2Fpusher%29&protocol=7&version=' . ApiSettings::getVersion(); + + self::assertSame($expectedUrl, ApiSettings::createUrl('barBaz','ap1')); } }