Fixed the subscribed topic names

This commit is contained in:
Alex Renoki 2020-08-24 14:06:58 +03:00
parent 3f8bb62291
commit c79bac07c4
1 changed files with 25 additions and 10 deletions

View File

@ -104,13 +104,13 @@ class RedisClient extends LocalClient
$payload = json_encode($payload); $payload = json_encode($payload);
$this->publishClient->__call('publish', ["{$appId}:{$channel}", $payload]); $this->publishClient->__call('publish', [$this->getTopicName($appId, $channel), $payload]);
DashboardLogger::log($appId, DashboardLogger::TYPE_REPLICATOR_MESSAGE_PUBLISHED, [ DashboardLogger::log($appId, DashboardLogger::TYPE_REPLICATOR_MESSAGE_PUBLISHED, [
'channel' => $channel, 'channel' => $channel,
'serverId' => $this->getServerId(), 'serverId' => $this->getServerId(),
'payload' => $payload, 'payload' => $payload,
'pubsub' => "{$appId}:{$channel}", 'pubsub' => $this->getTopicName($appId, $channel),
]); ]);
return true; return true;
@ -127,7 +127,7 @@ class RedisClient extends LocalClient
{ {
if (! isset($this->subscribedChannels["{$appId}:{$channel}"])) { if (! isset($this->subscribedChannels["{$appId}:{$channel}"])) {
// We're not subscribed to the channel yet, subscribe and set the count to 1 // We're not subscribed to the channel yet, subscribe and set the count to 1
$this->subscribeClient->__call('subscribe', ["{$appId}:{$channel}"]); $this->subscribeClient->__call('subscribe', [$this->getTopicName($appId, $channel)]);
$this->subscribedChannels["{$appId}:{$channel}"] = 1; $this->subscribedChannels["{$appId}:{$channel}"] = 1;
} else { } else {
// Increment the subscribe count if we've already subscribed // Increment the subscribe count if we've already subscribed
@ -137,7 +137,7 @@ class RedisClient extends LocalClient
DashboardLogger::log($appId, DashboardLogger::TYPE_REPLICATOR_SUBSCRIBED, [ DashboardLogger::log($appId, DashboardLogger::TYPE_REPLICATOR_SUBSCRIBED, [
'channel' => $channel, 'channel' => $channel,
'serverId' => $this->getServerId(), 'serverId' => $this->getServerId(),
'pubsub' => "{$appId}:{$channel}", 'pubsub' => $this->getTopicName($appId, $channel),
]); ]);
return true; return true;
@ -169,7 +169,7 @@ class RedisClient extends LocalClient
DashboardLogger::log($appId, DashboardLogger::TYPE_REPLICATOR_UNSUBSCRIBED, [ DashboardLogger::log($appId, DashboardLogger::TYPE_REPLICATOR_UNSUBSCRIBED, [
'channel' => $channel, 'channel' => $channel,
'serverId' => $this->getServerId(), 'serverId' => $this->getServerId(),
'pubsub' => "{$appId}:{$channel}", 'pubsub' => $this->getTopicName($appId, $channel),
]); ]);
return true; return true;
@ -194,7 +194,7 @@ class RedisClient extends LocalClient
'serverId' => $this->getServerId(), 'serverId' => $this->getServerId(),
'socketId' => $socketId, 'socketId' => $socketId,
'data' => $data, 'data' => $data,
'pubsub' => "{$appId}:{$channel}", 'pubsub' => $this->getTopicName($appId, $channel),
]); ]);
} }
@ -209,13 +209,13 @@ class RedisClient extends LocalClient
*/ */
public function leaveChannel($appId, string $channel, string $socketId) public function leaveChannel($appId, string $channel, string $socketId)
{ {
$this->publishClient->__call('hdel', ["{$appId}:{$channel}", $socketId]); $this->publishClient->__call('hdel', [$this->getTopicName($appId, $channel), $socketId]);
DashboardLogger::log($appId, DashboardLogger::TYPE_REPLICATOR_LEFT_CHANNEL, [ DashboardLogger::log($appId, DashboardLogger::TYPE_REPLICATOR_LEFT_CHANNEL, [
'channel' => $channel, 'channel' => $channel,
'serverId' => $this->getServerId(), 'serverId' => $this->getServerId(),
'socketId' => $socketId, 'socketId' => $socketId,
'pubsub' => "{$appId}:{$channel}", 'pubsub' => $this->getTopicName($appId, $channel),
]); ]);
} }
@ -228,7 +228,7 @@ class RedisClient extends LocalClient
*/ */
public function channelMembers($appId, string $channel): PromiseInterface public function channelMembers($appId, string $channel): PromiseInterface
{ {
return $this->publishClient->__call('hgetall', ["{$appId}:{$channel}"]) return $this->publishClient->__call('hgetall', [$this->getTopicName($appId, $channel)])
->then(function ($members) { ->then(function ($members) {
// The data is expected as objects, so we need to JSON decode // The data is expected as objects, so we need to JSON decode
return array_map(function ($user) { return array_map(function ($user) {
@ -249,7 +249,7 @@ class RedisClient extends LocalClient
$this->publishClient->__call('multi', []); $this->publishClient->__call('multi', []);
foreach ($channelNames as $channel) { foreach ($channelNames as $channel) {
$this->publishClient->__call('hlen', ["{$appId}:{$channel}"]); $this->publishClient->__call('hlen', [$this->getTopicName($appId, $channel)]);
} }
return $this->publishClient->__call('exec', []) return $this->publishClient->__call('exec', [])
@ -371,4 +371,19 @@ class RedisClient extends LocalClient
{ {
return $this->serverId; return $this->serverId;
} }
/**
* Get the Pub/Sub Topic name to subscribe based on the
* app ID and channel name.
*
* @param mixed $appId
* @param string $channel
* @return string
*/
protected function getTopicName($appId, string $channel): string
{
$prefix = config('database.redis.options.prefix', null);
return "{$prefix}{$appId}:{$channel}";
}
} }