forked from solid-connection/solid-connect-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStompWebSocketConfig.java
More file actions
68 lines (61 loc) · 3.38 KB
/
StompWebSocketConfig.java
File metadata and controls
68 lines (61 loc) · 3.38 KB
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
package com.example.solidconnection.chat.config;
import com.example.solidconnection.chat.config.StompProperties.HeartbeatProperties;
import com.example.solidconnection.chat.config.StompProperties.InboundProperties;
import com.example.solidconnection.chat.config.StompProperties.OutboundProperties;
import com.example.solidconnection.security.config.CorsProperties;
import java.util.List;
import java.util.Optional;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.ChannelRegistration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
@Configuration
@EnableWebSocketMessageBroker
@RequiredArgsConstructor
public class StompWebSocketConfig implements WebSocketMessageBrokerConfigurer {
private final StompHandler stompHandler;
private final StompProperties stompProperties;
private final CorsProperties corsProperties;
private final WebSocketHandshakeInterceptor webSocketHandshakeInterceptor;
private final CustomHandshakeHandler customHandshakeHandler;
private final Optional<WebSocketLoggingInterceptor> webSocketLoggingInterceptor;
@Override
public void registerStompEndpoints(StompEndpointRegistry registry) {
List<String> strings = corsProperties.allowedOrigins();
String[] allowedOrigins = strings.toArray(String[]::new);
registry.addEndpoint("/connect")
.setAllowedOrigins(allowedOrigins)
.addInterceptors(webSocketHandshakeInterceptor)
.setHandshakeHandler(customHandshakeHandler)
.withSockJS();
}
@Override
public void configureClientInboundChannel(ChannelRegistration registration) {
InboundProperties inboundProperties = stompProperties.threadPool().inbound();
webSocketLoggingInterceptor.ifPresent(registration::interceptors);
registration.interceptors(stompHandler)
.taskExecutor()
.corePoolSize(inboundProperties.corePoolSize())
.maxPoolSize(inboundProperties.maxPoolSize())
.queueCapacity(inboundProperties.queueCapacity());
}
@Override
public void configureClientOutboundChannel(ChannelRegistration registration) {
OutboundProperties outboundProperties = stompProperties.threadPool().outbound();
registration.taskExecutor().corePoolSize(outboundProperties.corePoolSize()).maxPoolSize(outboundProperties.maxPoolSize());
}
@Override
public void configureMessageBroker(MessageBrokerRegistry registry) {
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.setPoolSize(1);
scheduler.setThreadNamePrefix("wss-heartbeat-");
scheduler.initialize();
HeartbeatProperties heartbeatProperties = stompProperties.heartbeat();
registry.setApplicationDestinationPrefixes("/publish");
registry.enableSimpleBroker("/topic").setHeartbeatValue(new long[]{heartbeatProperties.serverInterval(), heartbeatProperties.clientInterval()}).setTaskScheduler(scheduler);
}
}