WebSocketConfig.java 1.91 KB
Newer Older
1
package web.multitask.trismegistoservices.config;
paolo committed
2 3

import org.springframework.context.annotation.Configuration;
4
import org.springframework.messaging.simp.config.ChannelRegistration;
paolo committed
5
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
6
import org.springframework.scheduling.annotation.Scheduled;
7
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
paolo committed
8 9 10
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
11
import web.multitask.trismegistoservices.filter.AuthChannelInterceptorAdapter;
paolo committed
12 13 14 15

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
16

17 18 19 20 21 22
    private final AuthChannelInterceptorAdapter authChannelInterceptorAdapter;

    public WebSocketConfig(AuthChannelInterceptorAdapter authChannelInterceptorAdapter) {
        this.authChannelInterceptorAdapter = authChannelInterceptorAdapter;
    }

paolo committed
23 24
    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
25 26 27 28 29 30 31 32
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(1);
        taskScheduler.setThreadNamePrefix("wss-heartbeat-thread-");
        taskScheduler.initialize();

        registry.enableSimpleBroker("/topic")
                .setHeartbeatValue(new long[]{10000, 10000})
                .setTaskScheduler(taskScheduler);
33
        registry.setApplicationDestinationPrefixes("/ws");
34

paolo committed
35 36 37 38 39 40
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("websocket").setAllowedOrigins("*");
    }
41 42 43 44 45

    @Override
    public void configureClientInboundChannel(ChannelRegistration registration) {
        registration.interceptors(authChannelInterceptorAdapter);
    }
paolo committed
46
}