WebSocketConfig.java 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. package com.steerinfo.dil.config;
  2. import org.springframework.context.annotation.Configuration;
  3. import org.springframework.messaging.simp.config.ChannelRegistration;
  4. import org.springframework.messaging.simp.config.MessageBrokerRegistry;
  5. import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
  6. import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
  7. import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;
  8. @Configuration
  9. @EnableWebSocketMessageBroker
  10. public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
  11. @Override
  12. public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
  13. stompEndpointRegistry
  14. .addEndpoint("/webSocket")
  15. .setAllowedOrigins("*")
  16. .withSockJS(); //使用sockJS
  17. }
  18. @Override
  19. public void configureMessageBroker(MessageBrokerRegistry registry) {
  20. //这里使用的是内存模式,生产环境可以使用rabbitmq或者其他mq。
  21. //这里注册两个,主要是目的是将广播和队列分开。
  22. //registry.enableStompBrokerRelay().setRelayHost().setRelayPort() 其他方式
  23. registry.enableSimpleBroker("/topic");
  24. //设置客户端前缀 即@MessageMapping
  25. registry.setApplicationDestinationPrefixes("/app");
  26. //点对点发送前缀
  27. registry.setUserDestinationPrefix("/user");
  28. }
  29. /**
  30. * 输入通道参数配置
  31. *
  32. * @param registration
  33. */
  34. @Override
  35. public void configureClientInboundChannel(ChannelRegistration registration) {
  36. }
  37. }