StompWebSocketConfig.java 1.87 KB
package com.huaheng.framework.config;

import com.huaheng.pc.stompwebsocket.SessionAuthHandshakeInterceptor;
import com.huaheng.pc.stompwebsocket.StompHandshakeHandler;
import com.huaheng.pc.stompwebsocket.WebsocketConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.scheduling.TaskScheduler;
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
public class StompWebSocketConfig implements WebSocketMessageBrokerConfigurer {
    @Autowired
    private TaskScheduler messageBrokerTaskScheduler;
    @Autowired
    private SessionAuthHandshakeInterceptor sessionAuthHandshakeInterceptor;

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {

//		config.enableStompBrokerRelay(WebsocketConstants.BROKER_TOPIC, WebsocketConstants.BROKER_QUEUE);
        config.enableSimpleBroker(WebsocketConstants.BROKER_TOPIC, WebsocketConstants.BROKER_QUEUE)
                .setHeartbeatValue(WebsocketConstants.HEART_BEAT)
                .setTaskScheduler(this.messageBrokerTaskScheduler);
        config.setUserDestinationPrefix(WebsocketConstants.USER_PRIFIX);
        config.setApplicationDestinationPrefixes(WebsocketConstants.APP_PRIFIX);
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint(WebsocketConstants.ENDPOINT)
                .setHandshakeHandler(new StompHandshakeHandler())
                .addInterceptors(sessionAuthHandshakeInterceptor)
                .withSockJS();
    }

}