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

69 lines
1.5 KiB
PHP
Raw Normal View History

2020-11-02 22:45:37 +08:00
<?php
declare(strict_types=1);
2020-12-26 21:33:40 +08:00
/**
* This is my open source code, please do not use it for commercial applications.
* For the full copyright and license information,
* please view the LICENSE file that was distributed with this source code
*
* @author Yuandong<837215079@qq.com>
* @link https://github.com/gzydong/hyperf-chat
*/
2020-11-02 22:45:37 +08:00
namespace App\Amqp\Producer;
2020-12-01 17:47:25 +08:00
use App\Constants\SocketConstants;
2020-11-02 22:45:37 +08:00
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-29 14:44:11 +08:00
/**
* 消息生产者
*
* @package App\Amqp\Producer
*/
2020-11-04 11:57:16 +08:00
class ChatMessageProducer extends ProducerMessage
2020-11-02 22:45:37 +08:00
{
2020-12-01 17:47:25 +08:00
/**
* 交换机类型
*
* @var string
*/
2020-11-02 22:45:37 +08:00
public $type = Type::FANOUT;
2020-12-01 17:47:25 +08:00
/**
* 交换机名称
*
* @var string
*/
public $exchange = SocketConstants::CONSUMER_MESSAGE_EXCHANGE;
2020-11-22 23:10:00 +08:00
/**
2020-12-01 17:47:25 +08:00
* 实例化处理
2020-11-22 23:10:00 +08:00
*
2021-04-20 16:30:57 +08:00
* @param string $event 事件名
* @param array $data 数据
* @param array $options 其它参数
2020-11-22 23:10:00 +08:00
*/
public function __construct(string $event, array $data, array $options = [])
2020-11-02 22:45:37 +08:00
{
2020-11-04 07:54:11 +08:00
$message = [
2021-04-22 16:14:34 +08:00
'uuid' => $this->uuid(),
2021-04-20 16:30:57 +08:00
'event' => $event,
'data' => $data,
2020-11-22 23:10:00 +08:00
'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
/**
2020-11-29 14:44:11 +08:00
* 生成唯一的消息ID
2020-11-07 22:57:10 +08:00
*
* @return string
*/
private function uuid()
{
2020-11-29 14:44:11 +08:00
return Str::random() . mt_rand(100000, 999999) . uniqid();
2020-11-07 22:57:10 +08:00
}
2020-11-02 22:45:37 +08:00
}