All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.github.dengchen2020.websocket.config.SpringWebSocketAutoConfiguration Maven / Gradle / Ivy

There is a newer version: 0.0.28
Show newest version
package io.github.dengchen2020.websocket.config;

import io.github.dengchen2020.websocket.annotation.WebSocketMapping;
import io.github.dengchen2020.websocket.properties.WebSocketProperties;
import jakarta.websocket.server.ServerEndpoint;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;
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.WebSocketHandlerRegistration;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.HandshakeHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import java.util.List;

/**
 * spring封装的websocket自动配置
 * @author dengchen
 * @since 2024/6/27
 */
@ConditionalOnProperty(value = "dc.websocket.enabled", matchIfMissing = true, havingValue = "true")
@EnableConfigurationProperties(WebSocketProperties.class)
@EnableWebSocket
@Configuration(proxyBeanMethods = false)
public class SpringWebSocketAutoConfiguration implements WebSocketConfigurer {

    private final List webSocketHandlers;

    private final HandshakeHandler handshakeHandler;

    private final HandshakeInterceptor[] handshakeInterceptors;

    private final WebSocketProperties webSocketProperties;

    public SpringWebSocketAutoConfiguration(@Nullable List webSocketHandlers, @Nullable HandshakeHandler handshakeHandler, @Nullable HandshakeInterceptor[] handshakeInterceptors, WebSocketProperties webSocketProperties) {
        this.webSocketHandlers = webSocketHandlers;
        this.handshakeHandler = handshakeHandler;
        this.handshakeInterceptors = handshakeInterceptors;
        this.webSocketProperties = webSocketProperties;
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        if (webSocketHandlers == null) return;
        for (WebSocketHandler handle : webSocketHandlers) {
            if (handle.getClass().getAnnotation(ServerEndpoint.class) != null) throw new RuntimeException("无需添加@ServerEndpoint注解," + handle.getClass().getName());
            WebSocketMapping webSocketMapping = handle.getClass().getAnnotation(WebSocketMapping.class);
            if (webSocketMapping == null) continue;
            WebSocketHandlerRegistration registration = registry.addHandler(handle, webSocketMapping.value());
            if(webSocketProperties.getAllowedOrigins() != null) registration.setAllowedOrigins(webSocketProperties.getAllowedOrigins());
            if(webSocketProperties.getAllowedOriginPatterns() != null) registration.setAllowedOriginPatterns(webSocketProperties.getAllowedOriginPatterns());
            if(webSocketProperties.getAllowedOrigins() == null && webSocketProperties.getAllowedOriginPatterns() == null) registration.setAllowedOriginPatterns("*");
            if(webSocketProperties.isWithSockJS()) registration.withSockJS();
            if(handshakeHandler != null) registration.setHandshakeHandler(handshakeHandler);
            if(handshakeInterceptors != null) registration.addInterceptors(handshakeInterceptors);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy