package com.sikey.wa04.business.application.service.websocket;

import io.nats.client.Connection;
import io.nats.client.Dispatcher;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @Author: qin
 * @Date: 2025/3/18
 * @Description: nats 订阅者
 */
@Slf4j
@Service
public class NatsSubscriberService {

    @Resource
    private Connection natsConnection;

    /**
     * 订阅消息
     *
     * @param topic
     * @return
     */
    public void subscribe(String topic) {
        // 订阅主题
        Dispatcher dispatcher = natsConnection.createDispatcher((msg) -> {
            String message = new String(msg.getData());
            log.info("Received message: {}", message);
        });

        dispatcher.subscribe(topic);
        log.info("Subscribed to topic: : {}", topic);
    }
}
