为不同的频道设置不同的WebSocket拦截器

时间:2018-11-09 02:43:29

标签: java spring

我的Spring Boot应用程序中具有以下websocket配置:

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfiguration implements WebSocketMessageBrokerConfigurer {

    public static final String WEBSOCKET_HANDSHAKE_ENDPOINT_URI = "/api/wsocket";
    public static final String QUOTE_CHANNEL_URI = "/quote";

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker(QUOTE_CHANNEL_URI); /*Enable a simple in-memory broker for the clients to subscribe to channels and receive messages*/
        config.setApplicationDestinationPrefixes("/app"); /*The prefix for the message mapping endpoints in the controller*/
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        /*websocket handshaking endpoint*/
        registry.addEndpoint(WEBSOCKET_HANDSHAKE_ENDPOINT_URI)
            /*TODO remove this after development*/
            .setAllowedOrigins("http://localhost:8080");
    }

    @Override
    public void configureClientInboundChannel(ChannelRegistration registeration) {
        registeration.interceptors(new SubscriptionInterceptor(rabbitTemplate));
    }
}

和以下拦截器:

public class SubscriptionInterceptor implements ChannelInterceptor {

    final private SubscriptionValidator[] validators;
    final private QuoteChannelServiceImpl quoteChannelService;

    public SubscriptionInterceptor(RabbitTemplate rabbitTemplate) {
        this.validators = new SubscriptionValidator[] { new QuoteSubscriptionValidator() };
        this.quoteChannelService = QuoteChannelServiceImpl.getInstance(rabbitTemplate);
    }

    @Override
    public Message<?> preSend(Message<?> message, MessageChannel channel) {
        StompHeaderAccessor headerAccessor = StompHeaderAccessor.wrap(message);
        StompCommand command = headerAccessor.getCommand();
        if (command.equals(StompCommand.SUBSCRIBE)) {
            validate(message);
            //if it is quote-request then
            quoteChannelService.subscriberJoined(headerAccessor.getDestination().replaceFirst(WebSocketConfiguration.QUOTE_CHANNEL_URI.concat("/"), ""));// TODO can we move "/" into the QUOTE_CHANNEL_URI?
            //TODO Can we split the interceptor to different ones, one for each broker?
        } else if (command.equals(StompCommand.UNSUBSCRIBE) || command.equals(StompCommand.DISCONNECT)) {
            quoteChannelService.subscriberLeft(headerAccessor.getDestination().replaceFirst(
                    WebSocketConfiguration.QUOTE_CHANNEL_URI.concat("/"), ""));// TODO can we move "/" into the QUOTE_CHANNEL_URI?          
        }
        return message;
    }

    private void validate(Message<?> message) {
        StompHeaderAccessor headerAccessor = StompHeaderAccessor.wrap(message);
        Arrays.stream(validators)
                .filter(validator -> validator.supports(headerAccessor)
                        && validator.validate(headerAccessor)).findFirst()
                .orElseThrow(() -> new MessagingException(message));
    }

}

我想知道我是否有可能为不同经纪人使用不同的拦截器。我

0 个答案:

没有答案