2021-07-07 19:43:09 +08:00
|
|
|
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace App\Process;
|
|
|
|
|
2021-07-11 21:10:54 +08:00
|
|
|
use App\Constants\RedisSubscribeChan;
|
2021-07-08 19:09:06 +08:00
|
|
|
use App\Service\Message\SubscribeHandleService;
|
2021-07-07 19:43:09 +08:00
|
|
|
use Hyperf\Process\AbstractProcess;
|
|
|
|
use Hyperf\Process\Annotation\Process;
|
|
|
|
|
|
|
|
/**
|
2021-07-09 22:57:19 +08:00
|
|
|
* Websocket 消息订阅处理服务
|
2021-07-07 19:43:09 +08:00
|
|
|
* @Process(name="RedisWebsocketSubscribe")
|
|
|
|
*/
|
|
|
|
class RedisWebsocketSubscribe extends AbstractProcess
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* 订阅的通道
|
|
|
|
*
|
|
|
|
* @var string[]
|
|
|
|
*/
|
2021-07-11 21:10:54 +08:00
|
|
|
private $chans = [
|
|
|
|
RedisSubscribeChan::WEBSOCKET_CHAN
|
|
|
|
];
|
2021-07-07 19:43:09 +08:00
|
|
|
|
|
|
|
/**
|
2021-07-08 19:09:06 +08:00
|
|
|
* @var SubscribeHandleService
|
2021-07-07 19:43:09 +08:00
|
|
|
*/
|
2021-07-08 19:09:06 +08:00
|
|
|
private $handleService;
|
2021-07-07 19:43:09 +08:00
|
|
|
|
2021-07-09 22:57:19 +08:00
|
|
|
/**
|
|
|
|
* 执行入口
|
|
|
|
*/
|
2021-07-07 19:43:09 +08:00
|
|
|
public function handle(): void
|
|
|
|
{
|
2021-07-23 21:34:29 +08:00
|
|
|
$this->handleService = di()->get(SubscribeHandleService::class);
|
2021-07-07 19:43:09 +08:00
|
|
|
|
|
|
|
redis()->subscribe($this->chans, [$this, 'subscribe']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 订阅处理逻辑
|
|
|
|
*
|
|
|
|
* @param $redis
|
|
|
|
* @param string $chan
|
|
|
|
* @param string $message
|
|
|
|
*/
|
|
|
|
public function subscribe($redis, string $chan, string $message)
|
|
|
|
{
|
|
|
|
//echo PHP_EOL . "chan : $chan , msg : $message";
|
|
|
|
$data = json_decode($message, true);
|
|
|
|
|
2022-01-17 21:06:27 +08:00
|
|
|
var_dump("subscribe ====> {$message}");
|
|
|
|
|
2021-07-09 22:57:19 +08:00
|
|
|
$this->handleService->handle($data);
|
2021-07-07 19:43:09 +08:00
|
|
|
}
|
|
|
|
}
|