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+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'helloworld.jsp' starting page</title>
   
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">   
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
<script type="text/javascript" src="<%=basePath%>js/jquery-1.3.2.min.js"></script>
<script type="text/javascript" src="<%=basePath%>js/comet4j.js"></script>
<script type="text/javascript" src="<%=basePath%>js/jquery-1.2.6.pack.js"></script>
<script type="text/javascript" src="<%=basePath%>js/jquery.messager.js"></script>
 <script type="text/javascript">
 
$(function(){
    JS.Engine.on('hello',function(text){ 
    //alert(text);//
其中text就是从后台传过来的数据
    $.messager.show('<font color=red>自定义标题</font>', text+'<font color=green style="font-size:14px;font-weight:bold;">自定义内容</font>');
    });
 JS.Engine.start('conn');
});
  
 
 
</script>
  </head>
 
  <body >      
      
  </body>
 
</html>

 

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

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    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">
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <listener>
        <description>Comet4J容器侦听</description>
        <listener-class>org.comet4j.core.CometAppListener</listener-class>
    </listener>
    <servlet>
        <description>Comet连接[默认:org.comet4j.core.CometServlet]</description>
        <display-name>CometServlet</display-name>
        <servlet-name>CometServlet</servlet-name>
        <servlet-class>org.comet4j.core.CometServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>CometServlet</servlet-name>
        <url-pattern>/conn</url-pattern>
    </servlet-mapping>
 
    <listener>
        <description>HelloWorld</description>
        <listener-class>org.comet4j.demo.helloworld.HelloWorld</listener-class>
    </listener>
</web-app>

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