package cn.reghao.tnb.search.esapp.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 logHandler;

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

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

        registry.addHandler(logHandler, WebSocketPath.wsAgent)
                .addInterceptors(webSocketInterceptor)
                .setAllowedOrigins("*");
    }
}
