package cn.reghao.tnb.message.app.ws.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

/**
 * @author reghao
 * @date 2022-03-11 16:43:25
 */
@EnableWebSocket
@Configuration
public class WebSocketConfig implements WebSocketConfigurer {
    private final WebSocketInterceptor webSocketInterceptor;
    private final WebSocketHandler progressHandler;
    private final WebSocketHandler chatHandler;
    private final WebSocketHandler deviceHandler;

    public WebSocketConfig(WebSocketInterceptor webSocketInterceptor,
                           @Qualifier("mediaHandler") WebSocketHandler progressHandler,
                           @Qualifier("chatHandler") WebSocketHandler chatHandler,
                           @Qualifier("deviceHandler") WebSocketHandler deviceHandler) {
        this.webSocketInterceptor = webSocketInterceptor;
        this.progressHandler = progressHandler;
        this.chatHandler = chatHandler;
        this.deviceHandler = deviceHandler;
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(progressHandler, WebSocketPath.wsMedia)
                .addInterceptors(webSocketInterceptor)
                .setAllowedOrigins("*");

        registry.addHandler(chatHandler, WebSocketPath.wsChat)
                .addInterceptors(webSocketInterceptor)
                .setAllowedOrigins("*");

        registry.addHandler(deviceHandler, WebSocketPath.wsDevice)
                .addInterceptors(webSocketInterceptor)
                .setAllowedOrigins("*");
    }
}
