package cn.edu.whu.boot.message.configure;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

/**
 * 1. WebSocketConfig is annotated with @Configuration to indicate that it is a Spring configuration class.
 * 2. @EnableWebSocketMessageBroker enables WebSocket message handling, backed by a message broker.
 */
@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {

        // Broker 和  App prefix
        registry.enableSimpleBroker("/topic/", "/location-broker", "/chat-broker");
        registry.setApplicationDestinationPrefixes("/app/");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/gs-guide-websocket").setAllowedOrigins("*").withSockJS();
        registry.addEndpoint("/location-sharing").setAllowedOrigins("*").withSockJS();
        registry.addEndpoint("/chat").setAllowedOrigins("*").withSockJS();

    }

}
