hyperf-chat/app/Process/RedisWebsocketSubscribe.php

57 lines
1.2 KiB
PHP
Raw Normal View History

2021-07-07 19:43:09 +08:00
<?php
declare(strict_types=1);
namespace App\Process;
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;
/**
* @Process(name="RedisWebsocketSubscribe")
*/
class RedisWebsocketSubscribe extends AbstractProcess
{
/**
* 订阅的通道
*
* @var string[]
*/
private $chans = ['websocket'];
/**
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
public function handle(): void
{
2021-07-08 19:09:06 +08:00
$this->handleService = container()->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);
2021-07-08 19:09:06 +08:00
if (!isset(SubscribeHandleService::EVENTS[$data['event']])) return;
$this->handleService->{SubscribeHandleService::EVENTS[$data['event']]}($data);
2021-07-07 19:43:09 +08:00
}
public function isEnable($server): bool
{
return true;
}
}