学习背景
好久之前就知道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