Spring4.0系列9-websocket简单应用

http://wiselyman.iteye.com/blog/2003336
*******************************************

Spring4.0系列1-新特性

Spring4.0系列2-环境搭建

Spring4.0系列3-@RestController

Spring4.0系列4-Meta Annotation(元注解)

Spring4.0系列5-@Conditional 

Spring4.0系列6-Generic Qualifier(泛型限定)

Spring4.0系列7-Ordering Autowired Collections

Spring4.0系列8-Groovy DSL

Spring4.0系列9-websocket简单应用

 

Spring 4.0的一个最大更新是增加了websocket的支持。websocket提供了一个在web应用中的高效、双向的通讯,需要考虑到客户端(浏览器)和服务器之间的高频和低延时消息交换。一般的应用场景有:在线交易、游戏、协作、数据可视化等。

 

使用websocket需要考虑的浏览器的支持(IE<10不支持),目前主流的浏览器都能很好的支持websocket。

websocket协议中有一些子协议,可以从更高的层次实现编程模型,就像我们使用HTTP而不是TCP一样。这些子协议有STOMP,WAMP等。

 

本教程只考虑websocket的简单实用,包含Spring对JSR-356的支持及Spring WebSocket API。

1、Java API for WebSocket(JSR-356)

Java API for WebSocket已经是Java EE 7的一部分。它定义了两类endpoit(都是EndPoint类的子类),使用注解标识@ClientEndpoint和@ServerEndpoint。

 

1.1 Servlet容器扫描初始化

通过Spring初始化一个endpoint,只需配置一个SpringConfigurator在类上的@ServerEndpoint注解上。

 

Java代码  收藏代码
  1. import javax.websocket.server.ServerEndpoint;  
  2. import org.springframework.web.socket.server.endpoint.SpringConfigurator;  
  3.   
  4.   
  5. @ServerEndpoint(value = "/echo", configurator = SpringConfigurator.class);  
  6. public class EchoEndpoint {  
  7.   
  8.   private final EchoService echoService;  
  9.   
  10.   @Autowired  
  11.   public EchoEndpoint(EchoService echoService) {  
  12.     this.echoService = echoService;  
  13.   }  
  14.   
  15.   @OnMessage  
  16.   public void handleMessage(Session session, String message) {  
  17.     // ...  
  18.   }  
  19.   
  20. }  

 上例假设SpringContextLoaderListener用来加载配置,这是个典型的web应用。Servlet容器将通过扫描@ServerEndpoint和SpringConfigurator初始化一个新的websocket会话。

 1.2 Spring 初始化

如果你想使用一个单独的实例而不使用Servlet容器扫描,将EchoEndpoint类声明称一个bean,并增加一个ServerEndpointExporter的bean:

Java代码  收藏代码
  1. import org.springframework.web.socket.server.endpoint.ServerEndpointExporter;  
  2.   
  3.   
  4. @Configuration  
  5. public class EndpointConfig {  
  6.   
  7.   @Bean  
  8.   public EchoEndpoint echoEndpoint() {  
  9.     return new EchoEndpoint(echoService());  
  10.   }  
  11.   
  12.   @Bean  
  13.   public EchoService echoService() {  
  14.     // ...  
  15.   }  
  16.   
  17.   @Bean  
  18.   public ServerEndpointExporter endpointExporter() {  
  19.     return new ServerEndpointExporter();  
  20.   }  
  21.   
  22. }  

 

 EchoEndpoint 可以通过EndPointRegistration发布:

Java代码  收藏代码
  1. import org.springframework.web.socket.server.endpoint.ServerEndpointExporter;  
  2. import org.springframework.web.socket.server.endpoint.ServerEndpointRegistration;  
  3.   
  4.   
  5. @Configuration  
  6. public class EndpointConfig {  
  7.   
  8.   @Bean  
  9.   public EndpointRegistration echoEndpoint() {  
  10.     return new EndpointRegistration("/echo", EchoEndpoint.class);  
  11.   }  
  12.   
  13.   @Bean  
  14.   public ServerEndpointExporter endpointExporter() {  
  15.     return new ServerEndpointExporter();  
  16.   }  
  17.   
  18.   // ..  
  19.   
  20. }  

 本例源码:spring-websocket-test-endpoint.zip

2、Spring WebSocket API

Spring WebSocket API提供了SockJS的支持,且有些容器如Jetty 9目前还没有对JSR-356的支持,所以有Spring WebSocket API是必要的。

 

Spring WebSocket API的核心接口是WebSocketHandler。下面是一个处理文本消息的handler的实现:

Java代码  收藏代码
  1. import org.springframework.web.socket.adapter.TextWebSocketHandlerAdapter;  
  2.   
  3.   
  4. public class EchoHandler extends TextWebSocketHandlerAdapter {  
  5.   
  6.   @Override  
  7.   public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {  
  8.     session.sendMessage(message);  
  9.   }  
  10.   
  11. }  

 

WebSocketHandler可以通过WebSocketHttpRequestHandler插入到Spring MVC里:

Java代码  收藏代码
  1. import org.springframework.web.socket.server.support.WebSocketHttpRequestHandler;  
  2.   
  3.   
  4. @Configuration  
  5. public class WebConfig {  
  6.   
  7.   @Bean  
  8.   public SimpleUrlHandlerMapping handlerMapping() {  
  9.   
  10.     Map<String, Object> urlMap = new HashMap<String, Object>();  
  11.     urlMap.put("/echo", new WebSocketHttpRequestHandler(new EchoHandler()));  
  12.   
  13.     SimpleUrlHandlerMapping hm = new SimpleUrlHandlerMapping();  
  14.     hm.setUrlMap(urlMap);  
  15.     return hm;  
  16.   }  
  17.   
  18. }  

 SockJS服务器端的支持

SockJs是一个脚本框架,它提供类似于websocket的编程模式但是可以适应不同的浏览器(包括不支持websocket的浏览器)。

 

开启SockJS的支持,声明一个SockJsService,和一个url映射,然后提供一个WebSocketHandler来处理消息。虽然 我们是哟个SockJS我们开发的方式是一样的,但是随着浏览器的不同传输的协议可以是Http Streaming,long polling等。

 

Java代码  收藏代码
  1. import org.springframework.web.socket.sockjs.SockJsService;  
  2. // ...  
  3.   
  4.   
  5. @Configuration  
  6. public class WebConfig {  
  7.   
  8.   @Bean  
  9.   public SimpleUrlHandlerMapping handlerMapping() {  
  10.   
  11.     SockJsService sockJsService = new DefaultSockJsService(taskScheduler());  
  12.   
  13.     Map<String, Object> urlMap = new HashMap<String, Object>();  
  14.     urlMap.put("/echo/**", new SockJsHttpRequestHandler(sockJsService, new EchoHandler()));  
  15.   
  16.     SimpleUrlHandlerMapping hm = new SimpleUrlHandlerMapping();  
  17.     hm.setUrlMap(urlMap);  
  18.     return hm;  
  19.   }  
  20.   
  21.   @Bean  
  22.   public ThreadPoolTaskScheduler taskScheduler() {  
  23.     ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();  
  24.     taskScheduler.setThreadNamePrefix("SockJS-");  
  25.     return taskScheduler;  
  26.   }  
  27.   
  28. }  

 

在我们实际使用中我们会使用WebSocketConfigurer集中注册WebSocket服务:

Java代码  收藏代码
  1. @Configuration  
  2. @EnableWebMvc  
  3. @EnableWebSocket//开启websocket  
  4. public class WebConfig extends WebMvcConfigurerAdapter implements WebSocketConfigurer {  
  5.   
  6.     @Override  
  7.     public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {  
  8.   
  9.         registry.addHandler(echoWebSocketHandler(), "/echo"); //提供符合W3C标准的Websocket数据  
  10.         registry.addHandler(snakeWebSocketHandler(), "/snake");  
  11.   
  12.         registry.addHandler(echoWebSocketHandler(), "/sockjs/echo").withSockJS();//提供符合SockJS的数据  
  13.         registry.addHandler(snakeWebSocketHandler(), "/sockjs/snake").withSockJS();  
  14.     }  
  15.   
  16.     @Bean  
  17.     public WebSocketHandler echoWebSocketHandler() {  
  18.         return new EchoWebSocketHandler(echoService());  
  19.     }  
  20.   
  21.     @Bean  
  22.     public WebSocketHandler snakeWebSocketHandler() {  
  23.         return new PerConnectionWebSocketHandler(SnakeWebSocketHandler.class);  
  24.     }  
  25.   
  26.     @Bean  
  27.     public DefaultEchoService echoService() {  
  28.         return new DefaultEchoService("Did you say \"%s\"?");  
  29.     }  
  30.   
  31.     // Allow serving HTML files through the default Servlet  
  32.   
  33.     @Override  
  34.     public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {  
  35.         configurer.enable();  
  36.     }  
  37.   
  38. }  

  

SockJS客户端代码:

Java代码  收藏代码
  1. ws = new SockJS(url, undefined, {protocols_whitelist: transports}) ;   //初始化 websocket  
  2.            
  3. ws.onopen = function () {  
  4.                 setConnected(true);  
  5.                 log('Info: connection opened.');  
  6.             };  
  7.   
  8. ws.onmessage = function (event) {  
  9.                 log('Received: ' + event.data); //处理服务端返回消息  
  10.             };  
  11.   
  12. ws.onclose = function (event) {  
  13.                 setConnected(false);  
  14.                 log('Info: connection closed.');  
  15.                 log(event);  
  16.             };  
  17.   
  18.  ws.send(message);//向服务端发送消息  

 

 程序用maven打成war后用tomcat 8发布查看效果。

本例源码:spring-websocket-test-master.zip

 

新书推荐《JavaEE开发的颠覆者: Spring Boot实战》,涵盖Spring 4.x、Spring MVC 4.x、Spring Boot企业开发实战。

 

京东地址:http://item.jd.com/11894632.html

当当地址:http://product.dangdang.com/23926195.html

亚马逊地址:http://www.amazon.cn/图书/dp/B01D5ZBFUK/ref=zg_bsnr_663834051_6 

淘宝地址:https://item.taobao.com/item.htm?id=528426235744&ns=1&abbucket=8#detail

 

或自己在京东、淘宝、亚马逊、当当、互动出版社搜索自选。

 


posted @ 2016-07-14 09:03  zhao1949  阅读(1739)  评论(0编辑  收藏  举报