package com.huashe.park.infrastructure.socket.cfg;

import java.util.Map;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationUtils;
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;
import org.springframework.web.socket.server.standard.ServletServerContainerFactoryBean;

import com.huashe.park.common.SpringBeanUtils;
import com.huashe.park.common.websocket.SocketEndPoint;

@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        Map<String, WebSocketHandler> beansOfType = SpringBeanUtils.getBeansOfType(WebSocketHandler.class);
        beansOfType.forEach((k, v) -> {
            SocketEndPoint annotation = AnnotationUtils.findAnnotation(v.getClass(), SocketEndPoint.class);
            registry.addHandler(v, annotation.url())
                // 添加拦截器，可以获取连接的param和 header 用作认证鉴权
                .addInterceptors(new SessionHandshakeInterceptor())
                // 设置运行跨域
                .setAllowedOrigins("*");
        });
    }

    @Bean
    public ServletServerContainerFactoryBean createWebSocketContainer() {
        ServletServerContainerFactoryBean container = new ServletServerContainerFactoryBean();
        // 设置默认会话空闲超时 以毫秒为单位 非正值意味着无限超时，默认值 0 ，默认没10s检查一次空闲就关闭
        container.setMaxSessionIdleTimeout(3600 * 1000L);
        // 设置异步发送消息的默认超时时间 以毫秒为单位 非正值意味着无限超时 ，默认值-1，还没看到作用
        container.setAsyncSendTimeout(10 * 1000L);
        // 设置文本消息的默认最大缓冲区大小 以字符为单位，默认值 8 * 1024
        container.setMaxTextMessageBufferSize(8 * 1024);
        // 设置二进制消息的默认最大缓冲区大小 以字节为单位，默认值 8 * 1024
        container.setMaxBinaryMessageBufferSize(8 * 1024);
        return container;
    }

}