com.healthy.common.websocket.config.WebSocketHandlerConfiguration Maven / Gradle / Ivy
package com.healthy.common.websocket.config;
import com.healthy.common.websocket.WebSocketProperties;
import com.healthy.common.websocket.handler.CustomWebSocketHandler;
import com.healthy.common.websocket.handler.PlanTextMessageHandler;
import com.healthy.common.websocket.session.DefaultWebSocketSessionStore;
import com.healthy.common.websocket.session.MapSessionWebSocketHandlerDecorator;
import com.healthy.common.websocket.session.SessionKeyGenerator;
import com.healthy.common.websocket.session.WebSocketSessionStore;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.socket.WebSocketHandler;
/**
* WebSocketHandlerConfig
*
* @author xm.z
*/
@RequiredArgsConstructor
public class WebSocketHandlerConfiguration {
private final WebSocketProperties webSocketProperties;
/**
* WebSocket session 存储器
* @param sessionKeyGenerator 唯一标识生成器
* @return DefaultWebSocketSessionStore
*/
@Bean
@ConditionalOnMissingBean
public WebSocketSessionStore webSocketSessionStore(
@Autowired(required = false) SessionKeyGenerator sessionKeyGenerator) {
return new DefaultWebSocketSessionStore(sessionKeyGenerator);
}
@Bean
@ConditionalOnMissingBean(WebSocketHandler.class)
public WebSocketHandler webSocketHandler(WebSocketSessionStore webSocketSessionStore,
@Autowired(required = false) PlanTextMessageHandler planTextMessageHandler) {
CustomWebSocketHandler customWebSocketHandler = new CustomWebSocketHandler(planTextMessageHandler);
if (webSocketProperties.isMapSession()) {
return new MapSessionWebSocketHandlerDecorator(customWebSocketHandler, webSocketSessionStore,
webSocketProperties.getConcurrent());
}
return customWebSocketHandler;
}
}