comet 推送 消息 聊天

阅读更多

 

需要的jar包以及基础的配置参考:

tomcat comet 推送 技术 入门 

直接切入正题:

 

首先引入tomcat的lib目录下的jar包:

 

catalina.jar,servlet-api.jar

 

另外要修改tomcat的server.xml,将protocal="http/1.1"什么的修改为:

 

 

 

这样就让tomcat支持推送功能了

 

首先是servlet(带有注释):

 

package nio.comet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.catalina.CometEvent;
import org.apache.catalina.CometProcessor;

public class ChatCometServlet extends HttpServlet implements CometProcessor{
	private static final long serialVersionUID = 1L;
	private List events = new ArrayList();

	@Override
	public void event(CometEvent event) throws IOException, ServletException {
		HttpServletRequest request = event.getHttpServletRequest();
		String path = request.getRequestURI();
		if(path.indexOf("setComet") != -1){
			//发送消息的请求
			String message = request.getParameter("message");
			//遍历所有的CometEvent,将消息发送出去
			synchronized(events){
				//使用list的遍历器
				Iterator iterator = events.iterator(); 
				CometEvent e = null;
				while(iterator.hasNext()){
					e = iterator.next();
					HttpServletResponse res = e.getHttpServletResponse();
					//设置响应的编码和类型
					res.setCharacterEncoding("utf-8");
					res.setContentType("text/plain");
					PrintWriter writer = res.getWriter();
					writer.write(message);
					writer.flush();
					//一定要调用list保存的CometEvent对象的close方法关闭,这样才可以及时将消息推送出去
					e.close();
					//删除的时候要使用iterator的删除方法,否则可能会引发cuncurrentModifyException
					iterator.remove();
				}
			}
			//最后关闭当前请求的CometEvent
			event.close();
			return;
		}else if(path.indexOf("getComet") != -1){
			//获取消息的请求
			if(event.getEventType() == CometEvent.EventType.BEGIN){
				//begin阶段,设置超时时间,并将CometEvent对于保存到list中,其他阶段均返回ERROR(简单处理)
				event.setTimeout(20000);
				synchronized(events){
					events.add(event);
				}
				return;
			}else if(event.getEventType() == CometEvent.EventType.END){
				synchronized(events){
					HttpServletResponse res = event.getHttpServletResponse();
					res.setCharacterEncoding("utf-8");
					res.setContentType("text/plain");
					PrintWriter writer = res.getWriter();
					writer.write("ERROR");
					writer.flush();
					event.close();
					events.remove(event);
				}
				return;
			}else if(event.getEventType() == CometEvent.EventType.ERROR){
				synchronized(events){
					HttpServletResponse res = event.getHttpServletResponse();
					res.setCharacterEncoding("utf-8");
					res.setContentType("text/plain");
					PrintWriter writer = res.getWriter();
					writer.write("ERROR");
					writer.flush();
					event.close();
					events.remove(event);
				}
				return;
			}else if(event.getEventType() == CometEvent.EventType.READ){
				synchronized(events){
					HttpServletResponse res = event.getHttpServletResponse();
					res.setCharacterEncoding("utf-8");
					res.setContentType("text/plain");
					PrintWriter writer = res.getWriter();
					writer.write("ERROR");
					writer.flush();
					event.close();
					events.remove(event);
				}
				return;
			}
		}
	}
}

 

 

然后再web.xml中配置servlet:

 

	
		mycomet
		nio.comet.ChatCometServlet
	
	
		mycomet
		/comet/*
	

 

然后就是页面了(里面需要引入jquery):

 

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


  
    
    My JSP 'chat.jsp' starting page
	
	
	    
	
	
	
	
  
  
    
消息:

 

然后就可以打开多个浏览器测试了。。。

 

 

你可能感兴趣的:(comet,推送,消息,聊天)