spring websocket + stomp 實現廣播通信和一對一通信<轉>


spring對於基於stomp協議的websocket通信,其官網上面有一個guide,但是根據guide你只能寫出來廣播方式的通信,不能實現一對一的通信,這篇文章在這里把廣播和一對一一起整理一下給大家。

服務端:

一,依賴,spring-websocket和spring-messaging,這里給出maven方式:

 

[html]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. <dependency>  
  2.             <groupId>org.springframework</groupId>  
  3.             <artifactId>spring-websocket</artifactId>  
  4.             <version>${spring-core.version}</version>  
  5.         </dependency>  
  6.         <dependency>  
  7.             <groupId>org.springframework</groupId>  
  8.             <artifactId>spring-messaging</artifactId>  
  9.             <version>${spring-core.version}</version>  
  10.         </dependency>  


二,服務端代碼:

 

服務端的初始化,只需要兩個類:WebsocketConfig和WebSocketController

 

[java]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. import org.springframework.context.annotation.Configuration;  
  2. import org.springframework.messaging.simp.config.MessageBrokerRegistry;  
  3. import org.springframework.web.socket.config.annotation.AbstractWebSocketMessageBrokerConfigurer;  
  4. import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;  
  5. import org.springframework.web.socket.config.annotation.StompEndpointRegistry;  
  6.   
  7. @Configuration  
  8. @EnableWebSocketMessageBroker  
  9. public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {  
  10.   
  11.     @Override  
  12.     public void configureMessageBroker(MessageBrokerRegistry config) {  
  13.         config.enableSimpleBroker("/topic","/user");  
  14.         config.setApplicationDestinationPrefixes("/app");  
  15.         config.setUserDestinationPrefix("/user/");  
  16.     }  
  17.   
  18.     @Override  
  19.     public void registerStompEndpoints(StompEndpointRegistry registry) {  
  20.         registry.addEndpoint("/webServer").withSockJS();  
  21.     }  
  22.   
  23. }  

 

 

這個類表示啟用websocket消息處理,以及收發消息的域

config.enableSimpleBroker("/topic","/user");這句表示在topic和user這兩個域上可以向客戶端發消息;config.setUserDestinationPrefix("/user/");這句表示給指定用戶發送(一對一)的主題前綴是“/user/”;  config.setApplicationDestinationPrefixes("/app"); 這句表示客戶端向服務端發送時的主題上面需要加"/app"作為前綴;registry.addEndpoint("/webServer").withSockJS();這個和客戶端創建連接時的url有關,后面在客戶端的代碼中可以看到。


下面是一個spring風格的controller,用於接收客戶端的消息及響應客戶端:

 

 

[java]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. import org.springframework.beans.factory.annotation.Autowired;  
  2. import org.springframework.messaging.handler.annotation.MessageMapping;  
  3. import org.springframework.messaging.handler.annotation.SendTo;  
  4. import org.springframework.messaging.simp.SimpMessagingTemplate;  
  5. import org.springframework.messaging.simp.annotation.SendToUser;  
  6. import org.springframework.stereotype.Controller;  
  7.   
  8. @Controller  
  9. public class WebSocketController {  
  10.   
  11.     public SimpMessagingTemplate template;  
  12.   
  13.     @Autowired  
  14.     public WebSocketController(SimpMessagingTemplate template) {  
  15.         this.template = template;  
  16.     }  
  17.   
  18.     @MessageMapping("/hello")  
  19.     @SendTo("/topic/hello")  
  20.     public Greeting greeting(Greeting message) throws Exception {  
  21.         return message;  
  22.     }  
  23.   
  24.     @MessageMapping("/message")  
  25.     @SendToUser("/message")  
  26.     public UserMessage userMessage(UserMessage userMessage) throws Exception {  
  27.         return userMessage;  
  28.     }  
  29.   
  30. }  

這個類里面織入SimpMessagingTemplate對象,后面動態發送消息時,需要用到這個對象。

 

第一個方法,表示服務端可以接收客戶端通過主題“/app/hello”發送過來的消息,客戶端需要在主題"/topic/hello"上監聽並接收服務端發回的消息

第二個方法道理相同,只是注意這里用的是@SendToUser,這就是發送給單一客戶端的標志。本例中,客戶端接收一對一消息的主題應該是“/user/” + 用戶Id + “/message” ,這里的用戶id可以是一個普通的字符串,只要每個用戶端都使用自己的id並且服務端知道每個用戶的id就行。

這里的Greeting和UserMessage是一個普通的實現了序列化的Java bean

就這樣,那么,在程序中的其他地方要動態發送消息,就是下面這兩句代碼:

 

[java]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. webSocketController.template.convertAndSend("/topic/hello",greeting) //廣播  
  2. webSocketController.template.convertAndSendToUser(userId, "/message",userMessage) //一對一發送,發送特定的客戶端  

 

 

 
        

 

客戶端:

js客戶端需要導入兩個js組件:sockjs-0.3.4.js,stomp.js

 

[javascript]  view plain  copy
 
 在CODE上查看代碼片派生到我的代碼片
  1. var url = 'http://localhost:8080/appRoot/webServer'  
  2. var socket = new SockJS(url, undefined, {protocols_whitelist: ['websocket']});  
  3. var stompClient = Stomp.over(socket);  
  4. stompClient.connect({}, function(frame) {  
  5.     stompClient.subscribe('/topic/hello', function(message){  
  6.         var json = JSON.parse(message.body);  
  7.     });  
  8.   
  9.      stompClient.subscribe('/user/' + userId + '/message', function(message){  
  10.          var messageEntity = JSON.parse(message.body);  
  11.     });  
  12. });  

 

第一個subscribe,是接收廣播消息,第二個是接收一對一消息,其主題是'/user/' + userId+ '/message' , 不同客戶端具有不同的userId

看到這里,你會發現,這里所謂的一對一,只是業務層面的一對一,也就是,需要不同客戶端具有不同的userId才能實現,如果兩個客戶端使用相同的userid, 那么服務端給這個userId發送消息時,這兩個客戶端都會收到。要真正實現websocket技術層面的一對一發送,那就要使用websocket的session了。關於session方式,我這里上傳一個demo,有興趣可以下載:http://download.csdn.net/detail/valenon/8725195


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM