首页 > 代码库 > springmvc实现long-pulling技术

springmvc实现long-pulling技术

背景介绍:

项目中有一个通讯模块,本来是用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>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<async-supported>true</async-supported>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	<!-- 配置spring-mvc -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<listener>
		<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
	</listener>
	<servlet>
		<servlet-name>SpringMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:spring-mvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
		<async-supported>true</async-supported>
	</servlet>
	<servlet-mapping>
		<servlet-name>SpringMVC</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>	
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
	
</web-app>
文件配置搞定。

2 建立控制器Controller

/**
 * @作者 yyp
 * @文件名 ChatController.java
 * @作用 处理聊天消息
 * @Blog http://blog.csdn.net/gisredevelopment
 */
@Controller
public class ChatController {
	//存放所有的用户请求
	private final Map<String, DeferredResult<Message>> chatRequests = new ConcurrentHashMap<String, DeferredResult<Message>>();
	//时间格式化
	private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	/**
	 * @作者 yyp
	 * @作用 登录
	 * @param name 用户名
	 * @param session 会话
	 * @return 聊天室页面
	 */
	@RequestMapping(value = http://www.mamicode.com/"/login", method = RequestMethod.POST)>

3 建立消息实体

/**
 * @作者 yyp
 * @文件名 Message.java
 * @作用 封装用户的聊天内容
 * @Blog http://blog.csdn.net/gisredevelopment
 */
public class Message {
	private String user;
	private String date;
	private String content;
}

4 页面代码-登录

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录</title>
</head>
<body>
<form action="login" method="post">
name: <input type="text" name="name"/>
<input value=http://www.mamicode.com/"登录" type="submit"/>>

5 页面代码-聊天

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<% String user =(String)session.getAttribute("user"); %>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>聊天室</title>
<script type="text/javascript" src=http://www.mamicode.com/"/imgr?src=http%3A%2F%2Fwww.ineeke.com%2Farchives%2F1486%2Fjquery-1.10.1.min.js"></script>>



springmvc实现long-pulling技术