package cn.reghao.bnt.web.ws.config;

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 2021-02-10 22:34:36
 */
@EnableWebSocket
@Configuration
public class WebSocketConfig implements WebSocketConfigurer {
    private final WebSocketInterceptor webSocketInterceptor;
    private final WebSocketHandler sshWebSocketHandler;
    private final WebSocketHandler agentWebSocketHandler;
    private final WebSocketHandler logHandler;

    public WebSocketConfig(WebSocketInterceptor webSocketInterceptor, WebSocketHandler sshWebSocketHandler, 
                           WebSocketHandler agentWebSocketHandler, WebSocketHandler logHandler) {
        this.webSocketInterceptor = webSocketInterceptor;
        this.sshWebSocketHandler = sshWebSocketHandler;
        this.agentWebSocketHandler = agentWebSocketHandler;
        this.logHandler = logHandler;
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        registry.addHandler(sshWebSocketHandler, WebSocketPath.wsSsh)
                .addInterceptors(webSocketInterceptor)
                .setAllowedOrigins("*");
        
        registry.addHandler(agentWebSocketHandler, WebSocketPath.wsAgent)
                .addInterceptors(webSocketInterceptor)
                .setAllowedOrigins("*");

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

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