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

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 videoProgressHandler;
    private final WebSocketHandler chatHandler;

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

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(videoProgressHandler, "/ws/progress")
                .addInterceptors(webSocketInterceptor)
                .setAllowedOrigins("*");

        registry.addHandler(chatHandler, "/ws/chat")
                .addInterceptors(webSocketInterceptor)
                .setAllowedOrigins("*");
    }
}
