package com.red.circle.game.fruit.infrastructure.websocket.endpoint;

import com.red.circle.game.fruit.infrastructure.websocket.message.MessageSender;
import com.red.circle.game.fruit.infrastructure.websocket.message.SocketMessageBody;
import com.red.circle.tool.core.json.JacksonUtils;
import java.security.Principal;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.stereotype.Controller;

/**
 * 消息服务.
 *
 * @author pengliang on 2021/7/19
 */
@Slf4j
@Controller
@RequiredArgsConstructor
public class SocketMessageEndpoint {

  private final MessageSender messageSender;

  /**
   * 广播发送消息，将消息发送到指定的目标地址. 将消息发送到 WebSocket 配置类中配置的代理中（/topic）进行消息转发.
   */
  @MessageMapping("/send-topic")
  public void sendTopicMessage(SocketMessageBody message, Principal principal) {
    log.warn("send-topic:{}", JacksonUtils.toJson(message), principal.getName());
    messageSender.send(message);
  }

  /**
   * 广播发送消息，将消息发送到指定的目标地址. 将消息发送到 WebSocket 配置类中配置的代理中（/queue）进行消息转发.
   */
  @MessageMapping("/send-queue")
  public void sendQueueMessage(SocketMessageBody message) {
    log.warn("send-queue: {}", JacksonUtils.toJson(message));
    messageSender.send(message);
  }

}
