yangys
2024-05-18 cc0bdfb33ef638dfafe3185c92c7076d815e1c9b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.qianwen.core.websocket.config;
 
import com.qianwen.core.websocket.custom.MasterlinkSessionKeyGenerator;
import com.qianwen.core.websocket.custom.UserAttributeHandshakeInterceptor;
import com.qianwen.core.websocket.handler.CustomPlanTextMessageHandler;
import com.qianwen.core.websocket.handler.CustomWebSocketHandler;
import com.qianwen.core.websocket.handler.PingJsonMessageHandler;
import com.qianwen.core.websocket.handler.PlanTextMessageHandler;
import com.qianwen.core.websocket.holder.MapSessionWebSocketHandlerDecorator;
import com.qianwen.core.websocket.holder.SessionKeyGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
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.web.socket.WebSocketHandler;
import org.springframework.web.socket.handler.TextWebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;
 
@EnableConfigurationProperties({WebSocketProperties.class})
public class WebSocketHandlerConfig {
    private final WebSocketProperties webSocketProperties;
 
    public WebSocketHandlerConfig(final WebSocketProperties webSocketProperties) {
        this.webSocketProperties = webSocketProperties;
    }
 
    @ConditionalOnMissingBean({SessionKeyGenerator.class})
    @Bean
    public SessionKeyGenerator sessionKeyGenerator() {
        return new MasterlinkSessionKeyGenerator();
    }
 
    @Bean
    public HandshakeInterceptor handshakeInterceptor() {
        return new UserAttributeHandshakeInterceptor();
    }
 
    @ConditionalOnMissingBean({PlanTextMessageHandler.class})
    @Bean
    public PlanTextMessageHandler planTextMessageHandler() {
        return new CustomPlanTextMessageHandler();
    }
 
    @ConditionalOnMissingBean({TextWebSocketHandler.class, PlanTextMessageHandler.class})
    @Bean
    public WebSocketHandler webSocketHandler1(@Autowired(required = false) SessionKeyGenerator sessionKeyGenerator) {
        CustomWebSocketHandler customWebSocketHandler = new CustomWebSocketHandler();
        if (this.webSocketProperties.isMapSession()) {
            return new MapSessionWebSocketHandlerDecorator(customWebSocketHandler, sessionKeyGenerator);
        }
        return customWebSocketHandler;
    }
 
    @ConditionalOnMissingBean({TextWebSocketHandler.class})
    @ConditionalOnBean({PlanTextMessageHandler.class})
    @Bean
    public WebSocketHandler webSocketHandler2(@Autowired(required = false) SessionKeyGenerator sessionKeyGenerator, PlanTextMessageHandler planTextMessageHandler) {
        CustomWebSocketHandler customWebSocketHandler = new CustomWebSocketHandler(planTextMessageHandler);
        if (this.webSocketProperties.isMapSession()) {
            return new MapSessionWebSocketHandlerDecorator(customWebSocketHandler, sessionKeyGenerator);
        }
        return customWebSocketHandler;
    }
 
    @ConditionalOnProperty(prefix = WebSocketProperties.PREFIX, name = {"heartbeat"}, havingValue = "true", matchIfMissing = true)
    @Bean
    public PingJsonMessageHandler pingJsonMessageHandler() {
        return new PingJsonMessageHandler();
    }
}