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注解上。

  1. /*
  2. * Copyright 2002-2013 the original author or authors.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.springframework.samples.websocket.config;
  17.  
  18. import org.springframework.context.annotation.Bean;
  19. import org.springframework.context.annotation.Configuration;
  20. import org.springframework.samples.websocket.echo.DefaultEchoService;
  21. import org.springframework.samples.websocket.echo.EchoEndpoint;
  22. import org.springframework.samples.websocket.echo.EchoService;
  23. import org.springframework.web.socket.server.standard.ServerEndpointExporter;
  24. import org.springframework.web.socket.server.standard.ServerEndpointRegistration;
  25.  
  26. @Configuration
  27. public class EndpointConfig {
  28.  
  29. @Bean
  30. public ServerEndpointExporter endpointExporter() {
  31. return new ServerEndpointExporter();
  32. }
  33.  
  34. @Bean
  35. public ServerEndpointRegistration echo() {
  36. return new ServerEndpointRegistration("/echo", EchoEndpoint.class);
  37. }
  38.  
  39. @Bean
  40. public ServerEndpointRegistration echoSingleton() {
  41. return new ServerEndpointRegistration("/echoSingleton", new EchoEndpoint(echoService()));
  42. }
  43.  
  44. // @Bean
  45. // public EchoAnnotatedEndpoint echoAnnotatedSingleton() {
  46. // return new EchoAnnotatedEndpoint(echoService());
  47. // }
  48.  
  49. @Bean
  50. public EchoService echoService() {
  51. return new DefaultEchoService("Did you say \"%s\"?");
  52. }
  53. }

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

1.2 Spring 初始化

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

EchoEndpoint 可以通过EndPointRegistration发布

2、Spring WebSocket API

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

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

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

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

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

SockJS服务器端的支持

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

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

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

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

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

SockJS客户端代码:

  1. <script type="text/javascript">
  2. var ws = null;
  3.  
  4. function setConnected(connected) {
  5. document.getElementById('connect').disabled = connected;
  6. document.getElementById('disconnect').disabled = !connected;
  7. document.getElementById('echo').disabled = !connected;
  8. }
  9.  
  10. function connect() {
  11. var target = document.getElementById('target').value;
  12. if (target == '') {
  13. alert('Please select server side connection implementation.');
  14. return;
  15. }
  16. if ('WebSocket' in window) {
  17. ws = new WebSocket(target);
  18. } else if ('MozWebSocket' in window) {
  19. ws = new MozWebSocket(target);
  20. } else {
  21. alert('WebSocket is not supported by this browser.');
  22. return;
  23. }
  24. ws.onopen = function () {
  25. setConnected(true);
  26. log('Info: WebSocket connection opened.');
  27. };
  28. ws.onmessage = function (event) {
  29. log('Received: ' + event.data);
  30. };
  31. ws.onclose = function () {
  32. setConnected(false);
  33. log('Info: WebSocket connection closed.');
  34. };
  35. }
  36.  
  37. function disconnect() {
  38. if (ws != null) {
  39. ws.close();
  40. ws = null;
  41. }
  42. setConnected(false);
  43. }
  44.  
  45. function echo() {
  46. if (ws != null) {
  47. var message = document.getElementById('message').value;
  48. log('Sent: ' + message);
  49. ws.send(message);
  50. } else {
  51. alert('WebSocket connection not established, please connect.');
  52. }
  53. }
  54.  
  55. function updateTarget(target) {
  56. if (window.location.protocol == 'http:') {
  57. document.getElementById('target').value = 'ws://' + window.location.host + target;
  58. } else {
  59. document.getElementById('target').value = 'wss://' + window.location.host + target;
  60. }
  61. }
  62.  
  63. function log(message) {
  64. var console = document.getElementById('console');
  65. var p = document.createElement('p');
  66. p.style.wordWrap = 'break-word';
  67. p.appendChild(document.createTextNode(message));
  68. console.appendChild(p);
  69. while (console.childNodes.length > 25) {
  70. console.removeChild(console.firstChild);
  71. }
  72. console.scrollTop = console.scrollHeight;
  73. }
  74. </script>

ws://localhost:8080/spring-websocket-test/echo

ws://localhost:8080/spring-websocket-test/echoSingleton

ws://localhost:8080/spring-websocket-test/echoAnnotated

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

  1. E:\myspace\spring-websocket-test-endpoint>mvn -DskipTests clean package

在target目录下生成了spring-websocket-test.war,部署到tomcat下,测试结果如下:

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

spring4.0之九:websocket简单应用的更多相关文章

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

    http://wiselyman.iteye.com/blog/2003336 ******************************************* Spring4.0系列1-新特性 ...

  2. Spring 4.0 中的 WebSocket 架构

    两年前,客户端与服务器端的全双工双向通信作为一个很重要的功能被纳入到WebSocket RFC 6455协议中.在HTML5中,WebSocket已经成为一个流行词,大家对这个功能赋予很多构想,很多时 ...

  3. [转]Struts2.3.16.1+Hibernate4.3.4+Spring4.0.2 框架整合

    原文地址:http://blog.csdn.net/ycb1689/article/details/22928519 最新版Struts2+Hibernate+Spring整合 目前为止三大框架最新版 ...

  4. Spring4.0编程式定时任务配置

    看过很多定时调度的配置,大多使用XML配置,觉得比较麻烦,也比较老套.这里介绍一种基于spring4.0注解编程式配置定时任务,简单清晰,使用方便.. 至于引入spring相关jar这里不多说,直接切 ...

  5. WebSocket简单介绍

    Java后端WebSocket的Tomcat实现 一.WebSocket简单介绍 随着互联网的发展,传统的HTTP协议已经很难满足Web应用日益复杂的需求了.近年来,随着HTML5的诞生,WebSoc ...

  6. [CXF REST标准实战系列] 二、Spring4.0 整合 CXF3.0,实现测试接口

    Writer:BYSocket(泥沙砖瓦浆木匠) 微博:BYSocket 豆瓣:BYSocket Reprint it anywhere u want. 文章Points: 1.介绍RESTful架构 ...

  7. Spring4.0之四:Meta Annotation(元注解)

    Spring框架自2.0开始添加注解的支持,之后的每个版本都增加了更多的注解支持.注解为依赖注入,AOP(如事务)提供了更强大和简便的方式.这也导致你要是用一个相同的注解到许多不同的类中去.这篇文章介 ...

  8. [CXF REST标准实战系列] 二、Spring4.0 整合 CXF3.0,实现测试接口(转)

    转自:[CXF REST标准实战系列] 二.Spring4.0 整合 CXF3.0,实现测试接口 文章Points: 1.介绍RESTful架构风格 2.Spring配置CXF 3.三层初设计,实现W ...

  9. websocket简单入门

    今天说起及时通信的时候,突然被问到时用推的方式,还是定时接受的方式,由于之前页面都是用传统的ajax处理,可能对ajax的定时获取根深蒂固了,所以一时之间没有相同怎么会出现推的方式呢?当被提及webs ...

随机推荐

  1. 嵌套for

  2. MAC安装python jupyter notebook

    介绍: Jupyter Notebook(此前被称为 IPython notebook)是一个交互式笔记本,支持运行 40 多种编程语言. Jupyter Notebook 的本质是一个 Web 应用 ...

  3. win10 ubuntu 同一硬盘双系统安装和启动设置

    1.了解启动的顺序 电脑开机--->  BIOS 设置 ----> 硬盘(MBR)/ GPT格式里的ESP分区 --->  (UEFI/GRUB)目录里的 *****.efi  -- ...

  4. LeetCode - Merge Two Binary Trees

    Given two binary trees and imagine that when you put one of them to cover the other, some nodes of t ...

  5. CSVN部署安装,实现web管理svn

    系统环境:centos7最小化安装 下载这个文件并解压 https://pan.baidu.com/s/1miwdBc8 tar zxvf jdk-8u91-linux-x64.gz mv jdk1. ...

  6. Cassandra--JAVA访问Cassandra数据

    JAVA创建Cluster对象 cluster = Cluster.builder() .addContactPoints(contactPoints) .withRetryPolicy(new Lo ...

  7. IAR intrinsic functions

    You can insert asm code example asm("NOP") into the c or c++ source code to get a good per ...

  8. 最新apache多域名多站点配置

    httpd.conf===> Listen Listen ServerName 用IP地址作为servername LoadModule rewrite_module modules/mod_r ...

  9. MyBatis 学习资料

    MyBatis 学习资料 table th:first-of-type { width: 90px; } table th:nth-of-type(2) { } table th:nth-of-typ ...

  10. 逻辑回归原理(python代码实现)

    Logistic Regression Classifier逻辑回归主要思想就是用最大似然概率方法构建出方程,为最大化方程,利用牛顿梯度上升求解方程参数. 优点:计算代价不高,易于理解和实现. 缺点: ...