diff --git a/server/.env.example b/server/.env.example index f86c802..a050097 100644 --- a/server/.env.example +++ b/server/.env.example @@ -19,6 +19,11 @@ REDIS_PORT=6379 REDIS_PASSWORD='' REDIS_DB=1 +# webman channel(用于定时任务通信) +WEBMAN_CHANNEL_HOST=127.0.0.1 +WEBMAN_CHANNEL_PORT=2207 +WEBMAN_CHANNEL_LISTEN_HOST=0.0.0.0 + # 游戏地址,用于 /api/v1/getGameUrl 返回 GAME_URL=dice-game-v3.yuliao666.top diff --git a/server/config/plugin/webman/channel/process.php b/server/config/plugin/webman/channel/process.php index c9a7d24..2ff9c2d 100644 --- a/server/config/plugin/webman/channel/process.php +++ b/server/config/plugin/webman/channel/process.php @@ -16,9 +16,15 @@ use Webman\Channel\Server; use Workerman\Protocols\Frame; +$listenHost = env('WEBMAN_CHANNEL_LISTEN_HOST', '0.0.0.0'); +$listenPort = filter_var(env('WEBMAN_CHANNEL_PORT'), FILTER_VALIDATE_INT); +if ($listenPort === false) { + $listenPort = 2207; +} + return [ 'server' => [ - 'listen' => 'frame://0.0.0.0:2207', + 'listen' => 'frame://' . $listenHost . ':' . $listenPort, 'protocol' => Frame::class, 'handler' => Server::class, 'reloadable' => false, diff --git a/server/plugin/saiadmin/app/logic/tool/CrontabLogic.php b/server/plugin/saiadmin/app/logic/tool/CrontabLogic.php index 994dd25..89cd9d8 100644 --- a/server/plugin/saiadmin/app/logic/tool/CrontabLogic.php +++ b/server/plugin/saiadmin/app/logic/tool/CrontabLogic.php @@ -20,6 +20,20 @@ use plugin\saiadmin\exception\ApiException; */ class CrontabLogic extends BaseLogic { + /** + * 获取 webman channel 服务地址 + * 需要保证与 server/config/plugin/webman/channel/process.php 的 listen 端口一致 + */ + private function channelConfig(): array + { + $host = env('WEBMAN_CHANNEL_HOST', '127.0.0.1'); + $port = filter_var(env('WEBMAN_CHANNEL_PORT'), FILTER_VALIDATE_INT); + if ($port === false) { + $port = 2207; + } + return ['host' => $host, 'port' => $port]; + } + /** * 构造函数 */ @@ -67,7 +81,8 @@ class CrontabLogic extends BaseLogic $id = $model->getKey(); // 连接到Channel服务 - ChannelClient::connect(); + $channel = $this->channelConfig(); + ChannelClient::connect($channel['host'], $channel['port']); ChannelClient::publish('crontab', ['args' => $id]); return true; @@ -116,7 +131,8 @@ class CrontabLogic extends BaseLogic ]); if ($result) { // 连接到Channel服务 - ChannelClient::connect(); + $channel = $this->channelConfig(); + ChannelClient::connect($channel['host'], $channel['port']); ChannelClient::publish('crontab', ['args' => $id]); } @@ -141,7 +157,8 @@ class CrontabLogic extends BaseLogic $result = parent::destroy($ids); if ($result) { // 连接到Channel服务 - ChannelClient::connect(); + $channel = $this->channelConfig(); + ChannelClient::connect($channel['host'], $channel['port']); ChannelClient::publish('crontab', ['args' => $ids]); } return $result; @@ -162,7 +179,8 @@ class CrontabLogic extends BaseLogic $result = $model->save(['status' => $status]); if ($result) { // 连接到Channel服务 - ChannelClient::connect(); + $channel = $this->channelConfig(); + ChannelClient::connect($channel['host'], $channel['port']); ChannelClient::publish('crontab', ['args' => $id]); } return $result; diff --git a/server/plugin/saiadmin/process/Task.php b/server/plugin/saiadmin/process/Task.php index dc31a59..c44ce4c 100644 --- a/server/plugin/saiadmin/process/Task.php +++ b/server/plugin/saiadmin/process/Task.php @@ -20,8 +20,13 @@ class Task $dbName = env('DB_NAME'); if (!empty($dbName)) { $this->logic = new CrontabLogic(); + $channelHost = env('WEBMAN_CHANNEL_HOST', '127.0.0.1'); + $channelPort = filter_var(env('WEBMAN_CHANNEL_PORT'), FILTER_VALIDATE_INT); + if ($channelPort === false) { + $channelPort = 2207; + } // 连接webman channel服务 - Client::connect(); + Client::connect($channelHost, $channelPort); // 订阅某个自定义事件并注册回调,收到事件后会自动触发此回调 Client::on('crontab', function ($data) { $this->reload($data);