Skip to content

Commit

Permalink
feat: 完善redis替代channel
Browse files Browse the repository at this point in the history
  • Loading branch information
chaz6chez committed Jun 11, 2024
1 parent 8492bae commit be7722e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
44 changes: 33 additions & 11 deletions src/ChannelClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Workbunny\WebmanPushServer;

use Exception;
use support\Redis;
use Workerman\Redis\Client;

class ChannelClient extends \Channel\Client
Expand All @@ -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);
}

/**
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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;
}
}
6 changes: 3 additions & 3 deletions src/config/plugin/workbunny/webman-push-server/app.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit be7722e

Please sign in to comment.