package com.market.config;

import com.market.websocket.SpotMarketWebSocketHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * 现货行情 WebSocket 配置
 * <p>
 * 注册处理器路径 /ws/spot，允许所有来源跨域连接。
 *
 * @author system
 * @date 2026-05-07
 */
@Configuration
@EnableWebSocket
public class SpotWebSocketConfig implements WebSocketConfigurer {

    private final SpotMarketWebSocketHandler spotMarketWebSocketHandler;

    public SpotWebSocketConfig(SpotMarketWebSocketHandler spotMarketWebSocketHandler) {
        this.spotMarketWebSocketHandler = spotMarketWebSocketHandler;
    }

    /**
     * 注册现货行情 WebSocket 处理器
     *
     * @param registry WebSocket 处理器注册表
     */
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(spotMarketWebSocketHandler, "/ws/spot")
                .setAllowedOrigins("*");
    }
}
