hyperf-chat/app/Amqp/Producer/ChatMessageProducer.php

57 lines
1.2 KiB
PHP
Raw Normal View History

2020-11-02 22:45:37 +08:00
<?php
declare(strict_types=1);
namespace App\Amqp\Producer;
use Hyperf\Amqp\Message\ProducerMessage;
use Hyperf\Amqp\Message\Type;
2020-11-07 22:57:10 +08:00
use Hyperf\Utils\Str;
2020-11-02 22:45:37 +08:00
2020-11-04 11:57:16 +08:00
class ChatMessageProducer extends ProducerMessage
2020-11-02 22:45:37 +08:00
{
public $exchange = 'im.message.fanout';
public $type = Type::FANOUT;
2020-11-22 23:10:00 +08:00
const EVENTS = [
'event_talk',
'event_keyboard',
'event_online_status',
'event_revoke_talk'
];
/**
* 初始化处理...
*
* @param string $event 事件名
* @param array $data 数据
* @param array $options 其它参数
*/
public function __construct(string $event, array $data, array $options = [])
2020-11-02 22:45:37 +08:00
{
2020-11-22 23:10:00 +08:00
if (!in_array($event, self::EVENTS)) {
new \Exception('事件名未注册...');
}
2020-11-04 07:54:11 +08:00
$message = [
2020-11-22 23:10:00 +08:00
'uuid' => $this->uuid(),// 自定义消息ID
'event' => $event,
'data' => $data,
'options' => $options
2020-11-04 07:54:11 +08:00
];
2020-11-08 17:10:05 +08:00
$this->payload = $message;
2020-11-02 22:45:37 +08:00
}
2020-11-07 22:57:10 +08:00
/**
* 生成唯一ID
*
* @return string
*/
private function uuid()
{
2020-11-08 22:58:17 +08:00
return Str::random() . rand(100000, 999999) . uniqid();
2020-11-07 22:57:10 +08:00
}
2020-11-02 22:45:37 +08:00
}