package cn.reghao.tnb.message.app.rabbit;

import cn.reghao.jutil.jdk.serializer.JsonConverter;
import cn.reghao.tnb.message.api.dto.MediaProgress;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

/**
 * @author reghao
 * @date 2024-04-19 14:38:30
 */
@Component
public class RabbitProducer {
    private final RabbitTemplate rabbitTemplate;

    public RabbitProducer(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    public void broadcastWsMessage(String msg) {
        String exchange = "amq.fanout";
        String routingKey = "";
        //如果交换机名称写错了。那么confirmCallback 回调失败的。
        //如果设置routingkey 是一个错误,那么returncallback 会进行回调调用
        rabbitTemplate.convertAndSend(exchange, routingKey, msg);
    }

    public void sendMediaProgress(MediaProgress mediaProgress) {
        String routingKey = "tnb.content.media";
        String jsonPayload = JsonConverter.objectToJson(mediaProgress);
        rabbitTemplate.convertAndSend(routingKey, jsonPayload);
    }
}
