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

com.healthy.common.websocket.WebSocketAutoConfiguration Maven / Gradle / Ivy

There is a newer version: 2.0.0-M1
Show newest version
package com.healthy.common.websocket;

import com.healthy.common.websocket.config.LocalMessageDistributorConfiguration;
import com.healthy.common.websocket.config.RedisMessageDistributorConfiguration;
import com.healthy.common.websocket.config.WebSocketHandlerConfiguration;
import com.healthy.common.websocket.handler.JsonMessageHandler;
import com.healthy.common.websocket.handler.PingJsonMessageHandler;
import com.healthy.common.websocket.holder.JsonMessageHandlerInitializer;
import com.healthy.common.websocket.message.JsonWebSocketMessage;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.SockJsServiceRegistration;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistration;
import org.springframework.web.socket.server.HandshakeInterceptor;

import java.util.List;

/**
 * websocket自动配置
 *
 * @author xm.z
 */
@Import({ WebSocketHandlerConfiguration.class, LocalMessageDistributorConfiguration.class,
		RedisMessageDistributorConfiguration.class })
@EnableWebSocket
@RequiredArgsConstructor
@EnableConfigurationProperties(WebSocketProperties.class)
public class WebSocketAutoConfiguration {

	private final WebSocketProperties webSocketProperties;

	@Bean
	@ConditionalOnMissingBean
	public WebSocketConfigurer webSocketConfigurer(List handshakeInterceptor,
			WebSocketHandler webSocketHandler,
			@Autowired(required = false) SockJsServiceConfigurer sockJsServiceConfigurer) {
		return registry -> {
			WebSocketHandlerRegistration registration = registry
					.addHandler(webSocketHandler, webSocketProperties.getPath())
					.addInterceptors(handshakeInterceptor.toArray(new HandshakeInterceptor[0]));

			String[] allowedOrigins = webSocketProperties.getAllowedOrigins();
			if (allowedOrigins != null && allowedOrigins.length > 0) {
				registration.setAllowedOrigins(allowedOrigins);
			}

			String[] allowedOriginPatterns = webSocketProperties.getAllowedOriginPatterns();
			if (allowedOriginPatterns != null && allowedOriginPatterns.length > 0) {
				registration.setAllowedOriginPatterns(allowedOriginPatterns);
			}

			if (webSocketProperties.isWithSockjs()) {
				SockJsServiceRegistration sockJsServiceRegistration = registration.withSockJS();
				if (sockJsServiceConfigurer != null) {
					sockJsServiceConfigurer.config(sockJsServiceRegistration);
				}
			}
		};
	}

	/**
	 * 心跳处理器
	 * @return PingJsonMessageHandler
	 */
	@Bean
	@ConditionalOnProperty(prefix = WebSocketProperties.PREFIX, name = "heartbeat", havingValue = "true",
			matchIfMissing = true)
	public PingJsonMessageHandler pingJsonMessageHandler() {
		return new PingJsonMessageHandler();
	}

	/**
	 * 注册 JsonMessageHandlerInitializer 收集所有的 json 类型消息处理器
	 * @param jsonMessageHandlerList json 类型消息处理器
	 * @return JsonMessageHandlerInitializer
	 */
	@Bean
	@ConditionalOnMissingBean
	public JsonMessageHandlerInitializer jsonMessageHandlerInitializer(
			List> jsonMessageHandlerList) {
		return new JsonMessageHandlerInitializer(jsonMessageHandlerList);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy