Spring WebSocket教程(一)

学习背景

好久之前就知道WebSocket,但那时不管是浏览器仍是开发技术对它的支持都还不多。可是,Spring4忽然发布,让我眼前一亮,Spring4直接支持WebSocket。
对于Spring我仍是很喜欢的,它让Java Web开发至关的有艺术感,此次支持的WebSocket又特别的和个人胃口,因此立刻就去学习了。

前提

本文的内容,是创建在懂J2EE编程,使用过Spring,据说过WebSocket上的,若是前面的3点你们不太明白,能够先去补补知识。
WebSocket还不是很广泛,对服务器和浏览器都有要求,不过使用了下面的一些技术,能够将浏览器的要求下降,可是服务器容器的要求仍是比较高的,具体哪些服务器容易支持了WebSocket能够百度一下。

第一步:配置Spring

若是你跟我同样采用的Maven,那么只须要将下面的几个依赖,加入到pom.xml文件就能够了:
[html]  view plain  copy
 
  1. <properties>  
  2.     <spring.version>4.0.0.RELEASE</spring.version>  
  3. </properties>  
  4.   
  5. <dependencies>  
  6.     <!--spring MVC-->  
  7.     <dependency>  
  8.         <groupId>org.springframework</groupId>  
  9.         <artifactId>spring-core</artifactId>  
  10.         <version>${spring.version}</version>  
  11.     </dependency>  
  12.   
  13.     <dependency>  
  14.         <groupId>org.springframework</groupId>  
  15.         <artifactId>spring-web</artifactId>  
  16.         <version>${spring.version}</version>  
  17.     </dependency>  
  18.   
  19.     <dependency>  
  20.         <groupId>org.springframework</groupId>  
  21.         <artifactId>spring-webmvc</artifactId>  
  22.         <version>${spring.version}</version>  
  23.     </dependency>  
  24.   
  25.     <!-- jstl -->  
  26.     <dependency>  
  27.         <groupId>jstl</groupId>  
  28.         <artifactId>jstl</artifactId>  
  29.         <version>1.2</version>  
  30.     </dependency>  
  31.   
  32.     <!--spring测试框架-->  
  33.     <dependency>  
  34.         <groupId>org.springframework</groupId>  
  35.         <artifactId>spring-test</artifactId>  
  36.         <version>${spring.version}</version>  
  37.         <scope>test</scope>  
  38.     </dependency>  
  39.   
  40.     <!--spring数据库操做库-->  
  41.     <dependency>  
  42.         <groupId>org.springframework</groupId>  
  43.         <artifactId>spring-jdbc</artifactId>  
  44.         <version>${spring.version}</version>  
  45.     </dependency>  
  46.   
  47.     <dependency>  
  48.         <groupId>junit</groupId>  
  49.         <artifactId>junit</artifactId>  
  50.         <version>4.8.2</version>  
  51.         <scope>test</scope>  
  52.     </dependency>  
  53.   
  54.     <!--spring websocket库-->  
  55.     <dependency>  
  56.         <groupId>org.springframework</groupId>  
  57.         <artifactId>spring-websocket</artifactId>  
  58.         <version>${spring.version}</version>  
  59.     </dependency>  
  60.     <dependency>  
  61.         <groupId>org.springframework</groupId>  
  62.         <artifactId>spring-messaging</artifactId>  
  63.         <version>${spring.version}</version>  
  64.     </dependency>  
  65.   
  66.     <!--jackson用于json操做-->  
  67.     <dependency>  
  68.         <groupId>com.fasterxml.jackson.core</groupId>  
  69.         <artifactId>jackson-databind</artifactId>  
  70.         <version>2.3.0</version>  
  71.     </dependency>  
  72.   
  73.     <dependency>  
  74.         <groupId>commons-fileupload</groupId>  
  75.         <artifactId>commons-fileupload</artifactId>  
  76.         <version>1.2.2</version>  
  77.     </dependency>  
  78.     <dependency>  
  79.         <groupId>commons-io</groupId>  
  80.         <artifactId>commons-io</artifactId>  
  81.         <version>2.2</version>  
  82.     </dependency>  
  83.     <!--使用阿里的链接池-->  
  84.     <dependency>  
  85.         <groupId>com.alibaba</groupId>  
  86.         <artifactId>druid</artifactId>  
  87.         <version>1.0.4</version>  
  88.     </dependency>  
  89.     <!--mysql connector-->  
  90.     <dependency>  
  91.         <groupId>mysql</groupId>  
  92.         <artifactId>mysql-connector-java</artifactId>  
  93.         <version>5.1.29</version>  
  94.     </dependency>  
  95.   
  96. </dependencies>  

Spring的配置我就不一一贴出来了,能够去github看,地址我会贴在下面。

第二步:配置WebSocket

我采用的是使用Configurer类和 Annotation来进行WebSocket配置。
首先要建立一个类,继承WebSocketMessageBrokerConfigurer,而且在类上加上annotation:@Configuration和@EnableWebSocketMessageBroker。这样,Spring就会将这个类当作配置类,而且打开WebSocket。
[java]  view plain  copy
 
  1. @Configuration  
  2. @EnableWebSocketMessageBroker  
  3. public class WebSocketConfig implements WebSocketMessageBrokerConfigurer{  
  4.     @Override  
  5.     public void registerStompEndpoints(StompEndpointRegistry registry) {  
  6.         //添加这个Endpoint,这样在网页中就能够经过websocket链接上服务了  
  7.         registry.addEndpoint("/coordination").withSockJS();  
  8.     }  
  9.   
  10.     @Override  
  11.     public void configureMessageBroker(MessageBrokerRegistry config) {  
  12.         System.out.println("服务器启动成功");  
  13.         //这里设置的simple broker是指能够订阅的地址,也就是服务器能够发送的地址  
  14.         /** 
  15.          * userChat 用于用户聊天 
  16.          */  
  17.         config.enableSimpleBroker("/userChat");  
  18.         config.setApplicationDestinationPrefixes("/app");  
  19.     }  
  20.   
  21.     @Override  
  22.     public void configureClientInboundChannel(ChannelRegistration channelRegistration) {  
  23.     }  
  24.   
  25.     @Override  
  26.     public void configureClientOutboundChannel(ChannelRegistration channelRegistration) {  
  27.     }  
  28. }  

能够看到,在类中必须实现这四个方法。暂且只须要用到前两个,因此我来介绍一下,前两个方法中代码的意义。
第一个方法,是registerStompEndpoints,大意就是注册消息链接点(我本身的理解),因此咱们进行了链接点的注册:
[java]  view plain  copy
 
  1. registry.addEndpoint("/coordination").withSockJS();  
咱们加了一个叫coordination的链接点,在网页上咱们就能够经过这个连接来和服务器的WebSocket链接了。可是后面还有一句withSockJs,这是什么呢?SockJs是一个WebSocket的通讯js库,Spring对这个js库进行了后台的自动支持,也就是说,咱们若是使用SockJs,那么咱们就不须要对后台进行更多的配置,只须要加上这一句就能够了。
第二个方法,configureMessageBroker,大意是设置消息代理,也就是页面上用js来订阅的地址,也是咱们服务器往WebSocket端接收js端发送消息的地址。
[java]  view plain  copy
 
  1. config.enableSimpleBroker("/userChat");  
  2. config.setApplicationDestinationPrefixes("/app");  
首先,定义了一个链接点叫userChat,从名字能够看的出,最后我会作一个聊天的例子。而后,设置了一个应用程序访问地址的前缀,目的估计是为了和其余的普通请求区分开吧。也就是说,网页上要发送消息到服务器上的地址是/app/userChat。
说了这么多地址,估计你们都绕晕了,由于项目的整个雏形尚未出来,因此很混乱。因此接下来就配置js端。

第三步:配置Browser端

说了这么多地址,估计你们都绕晕了,由于项目的整个雏形尚未出来,因此很混乱。因此接下来就配置js端。
首先咱们要使用两个js库,一个是以前说过的SockJs,一个是stomp,这是一种通讯协议,暂时不介绍它,只须要知道是一种更方便更安全的发送消息的库就好了。
须要链接服务端的WebSocket:
[javascript]  view plain  copy
 
  1. var socket = new SockJS('/coordination');  
  2. var stompClient = Stomp.over(socket);  
  3. stompClient.connect('', '', function (frame) {});  
没错,就只须要两句话。有了这三句话,咱们就已经能够链接上了服务器。
使用SockJs还有一个好处,那就是对浏览器进行兼容,若是是IE11如下等对WebSocket支持很差的浏览器,SockJs会自动的将WebSocket降级到轮询(这个不知道的能够去百度一下),以前也说了,Spring对SockJs也进行了支持,也就是说,若是以前加了withSockJs那句代码,那么服务器也会自动的降级为轮询。(怎么样,是否是很兴奋,Spring这个特性太让人舒服了)
可是链接上了服务器,却没有进行任何的操做,因此下一步,咱们要在服务器端撰写响应和数据处理代码,在Browser端撰写消息发送和接收代码。固然,这是下一篇的内容了。

结语

这是个人毕业设计,个人毕业设计是一个在线协同备课系统,其中包含了聊天这个小功能,因此使用它来说解一下Spring WebSocket的使用。
我将代码放到了 github上,有兴趣的朋友能够去看看代码,由于涉及到了不少协同操做,因此代码比较复杂,若是仅仅想了解Spring WebSocket的朋友,仍是等个人下一篇文章吧。 大笑
github地址:https://github.com/xjyaikj/OnlinePreparation
 
转自 http://blog.csdn.net/xjyzxx/article/details/24182677
相关文章
相关标签/搜索