comet4j轮询

阅读更多

 

 

参考地址 :http://code.google.com/p/comet4j/wiki/HelloWorld#%E5%AE%A2%E6%88%B7%E7%AB%AF 这是配置TOMCAT的

     今天终于用comet4J实习了轮询,在过程中,明白了许多。一定要细心。我也终于可以完成这篇博客了。

首先是必须要的jar包和js文件,这些东西我会上传。

 helloworld.jsp:

<%@ page contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>



 
   
   
    My JSP 'helloworld.jsp' starting page
   
   
   
       
   
   
   




 
 
 
        
      
 
 

 

HelloWorld.java

package org.comet4j.demo.helloworld;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.comet4j.core.CometContext;

public class HelloWorld
implements ServletContextListener {
    private static final String CHANNEL = "hello";
    public void contextInitialized(ServletContextEvent arg0) {
        CometContext cc = CometContext.getInstance();
        cc.registChannel(CHANNEL);// 注册应用的channel
        Thread helloAppModule = new Thread(new HelloAppModule(),"helloworld");
        helloAppModule.setDaemon(true);
        helloAppModule.start();
    }

 

    public void contextDestroyed(ServletContextEvent arg0) {
    // TODO Auto-generated method stub
   
}
     
}

一定要继承 ServletContextListener,这个我弄了一上午,最后才发现时没继承。

HelloAppModule.java

package org.comet4j.demo.helloworld;

import org.comet4j.core.CometContext;
import org.comet4j.core.CometEngine;

public class HelloAppModule implements Runnable{
    private static final String CHANNEL = "hello";

    public void run() {
        while (true) {
            CometEngine engine = CometContext.getInstance().getEngine();
            engine.sendToAll(CHANNEL , "我来了!");
//传输数据
        }
    }

}
web.xml


    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
   
        index.jsp
   

   
        Comet4J容器侦听
        org.comet4j.core.CometAppListener
   

   
        Comet连接[默认:org.comet4j.core.CometServlet]
        CometServlet
        CometServlet
        org.comet4j.core.CometServlet
   

   
        CometServlet
        /conn
   

 
   
        HelloWorld
        org.comet4j.demo.helloworld.HelloWorld
   



  • comet4j.rar (133.3 KB)
  • 下载次数: 509

你可能感兴趣的:(comet4j,轮询)