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
相關文章
相關標籤/搜索