學習背景
好久之前就知道WebSocket,但那時不管是瀏覽器仍是開發技術對它的支持都還不多。可是,Spring4忽然發佈,讓我眼前一亮,Spring4直接支持WebSocket。
對於Spring我仍是很喜歡的,它讓Java Web開發至關的有藝術感,此次支持的WebSocket又特別的和個人胃口,因此立刻就去學習了。
前提
本文的內容,是創建在懂J2EE編程,使用過Spring,據說過WebSocket上的,若是前面的3點你們不太明白,能夠先去補補知識。
WebSocket還不是很廣泛,對服務器和瀏覽器都有要求,不過使用了下面的一些技術,能夠將瀏覽器的要求下降,可是服務器容器的要求仍是比較高的,具體哪些服務器容易支持了WebSocket能夠百度一下。
第一步:配置Spring
若是你跟我同樣採用的Maven,那麼只須要將下面的幾個依賴,加入到pom.xml文件就能夠了:
- <properties>
- <spring.version>4.0.0.RELEASE</spring.version>
- </properties>
-
- <dependencies>
-
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-core</artifactId>
- <version>${spring.version}</version>
- </dependency>
-
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-web</artifactId>
- <version>${spring.version}</version>
- </dependency>
-
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-webmvc</artifactId>
- <version>${spring.version}</version>
- </dependency>
-
-
- <dependency>
- <groupId>jstl</groupId>
- <artifactId>jstl</artifactId>
- <version>1.2</version>
- </dependency>
-
-
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-test</artifactId>
- <version>${spring.version}</version>
- <scope>test</scope>
- </dependency>
-
-
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-jdbc</artifactId>
- <version>${spring.version}</version>
- </dependency>
-
- <dependency>
- <groupId>junit</groupId>
- <artifactId>junit</artifactId>
- <version>4.8.2</version>
- <scope>test</scope>
- </dependency>
-
-
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-websocket</artifactId>
- <version>${spring.version}</version>
- </dependency>
- <dependency>
- <groupId>org.springframework</groupId>
- <artifactId>spring-messaging</artifactId>
- <version>${spring.version}</version>
- </dependency>
-
-
- <dependency>
- <groupId>com.fasterxml.jackson.core</groupId>
- <artifactId>jackson-databind</artifactId>
- <version>2.3.0</version>
- </dependency>
-
- <dependency>
- <groupId>commons-fileupload</groupId>
- <artifactId>commons-fileupload</artifactId>
- <version>1.2.2</version>
- </dependency>
- <dependency>
- <groupId>commons-io</groupId>
- <artifactId>commons-io</artifactId>
- <version>2.2</version>
- </dependency>
-
- <dependency>
- <groupId>com.alibaba</groupId>
- <artifactId>druid</artifactId>
- <version>1.0.4</version>
- </dependency>
-
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.29</version>
- </dependency>
-
- </dependencies>
Spring的配置我就不一一貼出來了,能夠去github看,地址我會貼在下面。
第二步:配置WebSocket
我採用的是使用Configurer類和 Annotation來進行WebSocket配置。
首先要建立一個類,繼承WebSocketMessageBrokerConfigurer,而且在類上加上annotation:@Configuration和@EnableWebSocketMessageBroker。這樣,Spring就會將這個類當作配置類,而且打開WebSocket。
- @Configuration
- @EnableWebSocketMessageBroker
- public class WebSocketConfig implements WebSocketMessageBrokerConfigurer{
- @Override
- public void registerStompEndpoints(StompEndpointRegistry registry) {
-
- registry.addEndpoint("/coordination").withSockJS();
- }
-
- @Override
- public void configureMessageBroker(MessageBrokerRegistry config) {
- System.out.println("服務器啓動成功");
-
-
- config.enableSimpleBroker("/userChat");
- config.setApplicationDestinationPrefixes("/app");
- }
-
- @Override
- public void configureClientInboundChannel(ChannelRegistration channelRegistration) {
- }
-
- @Override
- public void configureClientOutboundChannel(ChannelRegistration channelRegistration) {
- }
- }
能夠看到,在類中必須實現這四個方法。暫且只須要用到前兩個,因此我來介紹一下,前兩個方法中代碼的意義。
第一個方法,是registerStompEndpoints,大意就是註冊消息鏈接點(我本身的理解),因此咱們進行了鏈接點的註冊:
- registry.addEndpoint("/coordination").withSockJS();
咱們加了一個叫coordination的鏈接點,在網頁上咱們就能夠經過這個連接來和服務器的WebSocket鏈接了。可是後面還有一句withSockJs,這是什麼呢?SockJs是一個WebSocket的通訊js庫,Spring對這個js庫進行了後臺的自動支持,也就是說,咱們若是使用SockJs,那麼咱們就不須要對後臺進行更多的配置,只須要加上這一句就能夠了。
第二個方法,configureMessageBroker,大意是設置消息代理,也就是頁面上用js來訂閱的地址,也是咱們服務器往WebSocket端接收js端發送消息的地址。
- config.enableSimpleBroker("/userChat");
- config.setApplicationDestinationPrefixes("/app");
首先,定義了一個鏈接點叫userChat,從名字能夠看的出,最後我會作一個聊天的例子。而後,設置了一個應用程序訪問地址的前綴,目的估計是爲了和其餘的普通請求區分開吧。也就是說,網頁上要發送消息到服務器上的地址是/app/userChat。
說了這麼多地址,估計你們都繞暈了,由於項目的整個雛形尚未出來,因此很混亂。因此接下來就配置js端。
第三步:配置Browser端
說了這麼多地址,估計你們都繞暈了,由於項目的整個雛形尚未出來,因此很混亂。因此接下來就配置js端。
首先咱們要使用兩個js庫,一個是以前說過的SockJs,一個是stomp,這是一種通訊協議,暫時不介紹它,只須要知道是一種更方便更安全的發送消息的庫就好了。
須要鏈接服務端的WebSocket:
- var socket = new SockJS('/coordination');
- var stompClient = Stomp.over(socket);
- 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