package com.its.pis.websocket;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
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;

@Slf4j
@RequiredArgsConstructor
@Configuration
@EnableWebSocket
public class ItsWebSocketConfig implements WebSocketConfigurer {

    public static final String WS_ENDPOINT = "/ws/pis-c2f.do";

    private final ItsWebSocketHandler itsWebsocketHandler;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {

        registry.addHandler(this.itsWebsocketHandler, ItsWebSocketConfig.WS_ENDPOINT)
                //.setAllowedOrigins("*")
                .setAllowedOriginPatterns("*")
                .withSockJS()
                //.setSessionCookieNeeded(false)
        ;  // sockjs

        registry.addHandler(this.itsWebsocketHandler, ItsWebSocketConfig.WS_ENDPOINT)
                .setAllowedOriginPatterns("*")
                //.setAllowedOrigins("*")
        ; // 그냥 websocket 지원
    }

}
