https://www.cnblogs.com/cao-lei/p/14211746.html
https://www.cnblogs.com/domi22/p/9460299.html
springboot整合webSocket的使用
引入jar包
<dependency><!-- 5.引入websocket-->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-websocket</artifactId>
        </dependency>
1 配置config
| 1 2 3 4 5 6 7 8 9 10 11 12 13 | packagecom.test.domi.config;importorg.springframework.context.annotation.Configuration;importorg.springframework.messaging.simp.config.MessageBrokerRegistry;importorg.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;importorg.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;importorg.springframework.web.socket.config.annotation.StompEndpointRegistry;@Configuration// @EnableWebSocketMessageBroker注解用于开启使用STOMP协议来传输基于代理(MessageBroker)的消息,这时候控制器(controller)// 开始支持@MessageMapping,就像是使用@requestMapping一样。@EnableWebSocketMessageBrokerpublicclassWebSocketConfig extendsAbstractWebSocketMessageBrokerConfigurer { | 
/*将"/hello"路径注册为STOMP端点,这个路径与发送和接收消息的目的路径有所不同,这是一个端点,客户端在订阅或发布消息到目的地址前,要连接该端点,
* 即用户发送请求url="/applicationName/hello"与STOMP server进行连接。之后再转发到订阅url;
* PS:端点的作用——客户端在订阅或发布消息到目的地址前,要连接该端点。
| 1 2 3 4 5 | @OverridepublicvoidregisterStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {    //注册一个Stomp的节点(endpoint),并指定使用SockJS协议。记得设置跨域    stompEndpointRegistry.addEndpoint("/endpointAric").setAllowedOrigins("*").withSockJS();} | 
/**
* 配置了一个简单的消息代理,如果不重载,默认情况下回自动配置一个简单的内存消息代理,用来处理以"/topic"为前缀的消息。这里重载configureMessageBroker()方法,
* 消息代理将会处理前缀为"/topic"和"/queue"的消息。
| 1 2 3 4 5 6 7 8 9 10 11 12 |     @Override    publicvoidconfigureMessageBroker(MessageBrokerRegistry registry) {        //服务端发送消息给客户端的域,多个用逗号隔开        registry.enableSimpleBroker("/topic");        //定义一对一推送的时候前缀        //registry.setUserDestinationPrefix("/user");        //定义websoket前缀        //registry.setApplicationDestinationPrefixes("/ws-push");    }   } | 
2 controller
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | packagecom.test.domi.controller;importcom.google.common.collect.Lists;importcom.test.domi.service.impl.WebSocketService;importorg.springframework.messaging.handler.annotation.MessageMapping;importorg.springframework.messaging.handler.annotation.SendTo;importorg.springframework.messaging.simp.SimpMessagingTemplate;importorg.springframework.stereotype.Controller;importjavax.annotation.Resource;importjava.util.List;@ControllerpublicclassWsController {    @Resource    privateSimpMessagingTemplate template;    @MessageMapping("/welcome")//@MessageMapping和@RequestMapping功能类似,用于设置URL映射地址,浏览器向服务器发起请求,需要通过该地址。    publicvoidsay(String message) throwsException {        template.convertAndSend("/topic/getResponse", message);    } | 
 
                    
                     
                    
                 
                    
                 
         
