From be7722e2284934e60f04107b041c1d442c641f58 Mon Sep 17 00:00:00 2001 From: chaz6chez Date: Tue, 11 Jun 2024 17:54:10 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E5=96=84redis=E6=9B=BF?= =?UTF-8?q?=E4=BB=A3channel?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/ChannelClient.php | 44 ++++++++++++++----- .../workbunny/webman-push-server/app.php | 6 +-- 2 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/ChannelClient.php b/src/ChannelClient.php index d7d8868..8f1ca1b 100644 --- a/src/ChannelClient.php +++ b/src/ChannelClient.php @@ -3,6 +3,7 @@ namespace Workbunny\WebmanPushServer; use Exception; +use support\Redis; use Workerman\Redis\Client; class ChannelClient extends \Channel\Client @@ -16,8 +17,17 @@ class ChannelClient extends \Channel\Client */ public static function isChannelEnv(): bool { - return !class_exists("\Workerman\Redis\Client", false) and - !config('plugin.workbunny.webman-push-server.app.push-server.channel_substitution_enable'); + return + !class_exists("\Workerman\Redis\Client", false) and + !self::getChannelSubstitution(); + } + + /** + * @return false|string + */ + public static function getChannelSubstitution(): false|string + { + return config('plugin.workbunny.webman-push-server.app.push-server.channel_substitution_enable', false); } /** @@ -32,6 +42,20 @@ public static function connect($ip = '127.0.0.1', $port = 2206, array $options = if (self::isChannelEnv()) { parent::connect($ip, $port); } else { + $channelName = self::getChannelSubstitution(); + if (!$config = config('redis.' . $channelName)) { + throw new \InvalidArgumentException("Redis channel [$channelName] not found. "); + } + $ip = $config['host']; + $port = $config['port']; + self::$_redisClient = (new Client(self::$_redisAddress = "redis://$ip:$port", self::$_redisOptions = $options)); + self::$_redisClient->connect(); + if ($passport = $config['password'] ?? null) { + self::$_redisClient->auth($passport); + } + if ($database = $config['database'] ?? 0) { + self::$_redisClient->select($database); + } self::connectRedis("redis://$ip:$port", $options); } } @@ -108,15 +132,13 @@ protected static function connectRedis(string $address, array $options = []): vo */ protected static function sendAnywayByRedis(array $events, mixed $data): bool { - self::connectRedis(self::$_redisAddress, self::$_redisOptions); - if (self::$_isWorkermanEnv) { - $data = serialize($data); - foreach ($events as $event) { - self::$_redisClient?->publish("workbunny:webman-push-server:$event", $data); - } - return true; - } else { - throw new Exception('Not workerman env. '); + $channelName = self::getChannelSubstitution(); + if (!config('redis.' . $channelName)) { + throw new \InvalidArgumentException("Redis channel [$channelName] not found. "); + } + foreach ($events as $event) { + Redis::connection($channelName)->client()->publish("workbunny:webman-push-server:$event", serialize($data)); } + return true; } } \ No newline at end of file diff --git a/src/config/plugin/workbunny/webman-push-server/app.php b/src/config/plugin/workbunny/webman-push-server/app.php index 6b67528..433d9bd 100644 --- a/src/config/plugin/workbunny/webman-push-server/app.php +++ b/src/config/plugin/workbunny/webman-push-server/app.php @@ -36,11 +36,11 @@ 'redis_channel' => 'default', // 心跳检查,0为不检查 'heartbeat' => 60, - // 是否使用redis替代channel + // 是否使用redis替代channel 【false为关闭 | 具体的redis channel name为开启】 'channel_substitution_enable' => false, - // channel默认地址 + // channel默认地址 【启用redis替代后,该参数无意义】 'channel_host' => '127.0.0.1', - // channel默认端口 + // channel默认端口 【启用redis替代后,该参数无意义】 'channel_port' => 2206, // 验证app_key 'apps_query' => function (string $appKey, ?string $appId = null): array