Passing the Socket ID to the broadcastAcrossServers()

This commit is contained in:
Alex Renoki 2020-09-18 11:57:10 +03:00
parent 0f90f521e0
commit 30c6635a91
6 changed files with 15 additions and 8 deletions

View File

@ -45,7 +45,7 @@ class TriggerEvent extends Controller
); );
} else { } else {
$this->channelManager->broadcastAcrossServers( $this->channelManager->broadcastAcrossServers(
$request->appId, $channelName, (object) $payload $request->appId, $request->socket_id, $channelName, (object) $payload
); );
} }

View File

@ -259,11 +259,13 @@ class LocalChannelManager implements ChannelManager
* Broadcast the message across multiple servers. * Broadcast the message across multiple servers.
* *
* @param string|int $appId * @param string|int $appId
* @param string|null $socketId
* @param string $channel * @param string $channel
* @param stdClass $payload * @param stdClass $payload
* @param string|null $serverId
* @return bool * @return bool
*/ */
public function broadcastAcrossServers($appId, string $channel, stdClass $payload) public function broadcastAcrossServers($appId, ?string $socketId, string $channel, stdClass $payload, string $serverId = null)
{ {
return true; return true;
} }

View File

@ -268,14 +268,17 @@ class RedisChannelManager extends LocalChannelManager
* Broadcast the message across multiple servers. * Broadcast the message across multiple servers.
* *
* @param string|int $appId * @param string|int $appId
* @param string|null $socketId
* @param string $channel * @param string $channel
* @param stdClass $payload * @param stdClass $payload
* @param string|null $serverId
* @return bool * @return bool
*/ */
public function broadcastAcrossServers($appId, string $channel, stdClass $payload) public function broadcastAcrossServers($appId, ?string $socketId, string $channel, stdClass $payload, string $serverId = null)
{ {
$payload->appId = $appId; $payload->appId = $appId;
$payload->serverId = $this->getServerId(); $payload->socketId = $socketId;
$payload->serverId = $serverId ?: $this->getServerId();
$this->publishClient->publish($this->getRedisKey($appId, $channel), json_encode($payload)); $this->publishClient->publish($this->getRedisKey($appId, $channel), json_encode($payload));

View File

@ -130,7 +130,7 @@ class Channel
->each->send(json_encode($payload)); ->each->send(json_encode($payload));
if ($replicate) { if ($replicate) {
$this->channelManager->broadcastAcrossServers($appId, $this->getName(), $payload); $this->channelManager->broadcastAcrossServers($appId, null, $this->getName(), $payload);
} }
return true; return true;
@ -148,7 +148,7 @@ class Channel
public function broadcastToEveryoneExcept(stdClass $payload, ?string $socketId, $appId, bool $replicate = true) public function broadcastToEveryoneExcept(stdClass $payload, ?string $socketId, $appId, bool $replicate = true)
{ {
if ($replicate) { if ($replicate) {
$this->channelManager->broadcastAcrossServers($appId, $this->getName(), $payload); $this->channelManager->broadcastAcrossServers($appId, $socketId, $this->getName(), $payload);
} }
if (is_null($socketId)) { if (is_null($socketId)) {

View File

@ -131,11 +131,13 @@ interface ChannelManager
* Broadcast the message across multiple servers. * Broadcast the message across multiple servers.
* *
* @param string|int $appId * @param string|int $appId
* @param string|null $socketId
* @param string $channel * @param string $channel
* @param stdClass $payload * @param stdClass $payload
* @param string|null $serverId
* @return bool * @return bool
*/ */
public function broadcastAcrossServers($appId, string $channel, stdClass $payload); public function broadcastAcrossServers($appId, ?string $socketId, string $channel, stdClass $payload, string $serverId = null);
/** /**
* Handle the user when it joined a presence channel. * Handle the user when it joined a presence channel.

View File

@ -90,7 +90,7 @@ class DashboardLogger
); );
} else { } else {
$channelManager->broadcastAcrossServers( $channelManager->broadcastAcrossServers(
$appId, $channelName, (object) $payload $appId, null, $channelName, (object) $payload
); );
} }
} }