• 大小: 132KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-21
  • 语言: 其他
  • 标签: comet4j  聊天  

资源简介

基于comet4j的简易聊天

资源截图

代码片段和文件信息

package org.comet4j.demo.helloworld;

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();  
                   

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2013-10-24 17:54  HelloWorld_\
     文件         834  2013-10-24 17:54  HelloWorld_\.classpath
     文件        1044  2013-10-24 17:54  HelloWorld_\.project
     目录           0  2013-10-24 17:57  HelloWorld_\.settings\
     文件         503  2013-10-24 17:54  HelloWorld_\.settings\.jsdtscope
     文件         104  2013-10-24 17:57  HelloWorld_\.settings\org.eclipse.core.resources.prefs
     文件         395  2013-10-24 17:54  HelloWorld_\.settings\org.eclipse.jdt.core.prefs
     文件         487  2013-10-24 17:54  HelloWorld_\.settings\org.eclipse.wst.common.component
     文件         345  2013-10-24 17:54  HelloWorld_\.settings\org.eclipse.wst.common.project.facet.core.xml
     文件          49  2013-10-24 17:54  HelloWorld_\.settings\org.eclipse.wst.jsdt.ui.superType.container
     文件           6  2013-10-24 17:54  HelloWorld_\.settings\org.eclipse.wst.jsdt.ui.superType.name
     目录           0  2013-10-25 14:00  HelloWorld_\WebContent\
     目录           0  2013-10-24 17:54  HelloWorld_\WebContent\meta-INF\
     文件          39  2013-10-24 17:54  HelloWorld_\WebContent\meta-INF\MANIFEST.MF
     目录           0  2013-10-24 17:54  HelloWorld_\WebContent\WEB-INF\
     目录           0  2013-10-25 13:59  HelloWorld_\WebContent\WEB-INF\lib\
     文件      108103  2013-10-24 17:11  HelloWorld_\WebContent\WEB-INF\lib\comet4j-tomcat6.jar
     文件        1824  2013-10-25 14:01  HelloWorld_\WebContent\WEB-INF\web.xml
     文件        1814  2013-10-25 14:02  HelloWorld_\WebContent\chat.jsp
     文件         888  2013-10-25 09:16  HelloWorld_\WebContent\hello.html
     目录           0  2013-10-25 13:47  HelloWorld_\WebContent\js\
     文件       20107  2013-10-24 17:15  HelloWorld_\WebContent\js\comet4j.js
     文件       57254  2013-09-24 09:05  HelloWorld_\WebContent\js\jquery.js
     目录           0  2013-10-24 17:54  HelloWorld_\build\
     目录           0  2013-10-25 13:59  HelloWorld_\build\classes\
     目录           0  2013-10-25 13:59  HelloWorld_\build\classes\org\
     目录           0  2013-10-25 13:59  HelloWorld_\build\classes\org\comet4j\
     目录           0  2013-10-25 13:59  HelloWorld_\build\classes\org\comet4j\demo\
     目录           0  2013-10-25 13:59  HelloWorld_\build\classes\org\comet4j\demo\helloworld\
     文件        3619  2013-10-25 13:59  HelloWorld_\build\classes\org\comet4j\demo\helloworld\ChatCometServlet.class
     文件        1374  2013-10-25 13:59  HelloWorld_\build\classes\org\comet4j\demo\helloworld\HelloWorld$HelloAppModule.class
............此处省略8个文件信息

评论

共有 条评论