背景介绍:
项目中有一个通讯模块,本来是用websocket全双工技术实现的,但IE10以下不支持websocket,而国内的360、2345浏
览器封装的全部是IE10以下的内核,考虑到网站在国内的客户,不得不在不支持websocket时候也要提供通讯支持,于
是决定在不支持websocket的浏览器上用long-pulling技术替代。
可行性分析:
Servlet 3.0已经开始支持async,Spring MVC 3.2也开始对异步提供支持,于是结合DeferredResult来实现聊天技术。
具体实现:
1 文件配置:
假设你已经有了spring+springmvc框架,我们只需对配置文件做微小改动,要在web.xml中的所有的filter及servlet中需要声明使用async:
<async-supported>true</async-supported>
web.xml完整配置如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!-- 配置spring-mybatis.xml -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
<filter>
<filter-name>encodi