参考地址 :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(Comet for Java)是一个纯粹基于AJAX(XMLHTTPRequest)的服务器推送框架,消息以JSON方式传递,具备长轮询、长连接、自动选择三种工作模式 文件包含comet4j-tomcat6.jar , comet4j-tomcat7.jar , comet4j.js...
comet4j完整包、comet4j.js、comet4j-tomcat6.jar、comet4j-tomcat7.jar
解决comet4j在tomcat7一下问题,和在nginx环境下问题。
Comet4J(Comet for Java)是一个纯粹基于AJAX(XMLHTTPRequest)的服务器推送框架,消息以JSON方式传递,具备长轮询、长连接、自动选择三种工作模式。 功能特性 推送消息广播。 推送定向消息。 提供连接上线前、上线、...
Comet4J Comet for Java 是一个纯粹基于AJAX XMLHTTPRequest 的服务器推送框架 消息以JSON方式传递 具备长轮询 长连接 自动选择三种工作模式
Comet4J(Comet for Java)是一个纯粹基于AJAX(XMLHTTPRequest)的服务器推送框架,消息以JSON方式传递,具备长轮询、长连接、自动选择三种工作模式。
comet4jAutomatically exported from code.google.com/p/comet4j功能特性推送消息广播。推送定向消息。提供连接上线前、上线、下线前、下线、发送消息等多种可处理事件。消息缓存机制,确保长轮询工作模式下不丢失...
Comet4J(Comet for Java)是一个纯粹基于AJAX(XMLHTTPRequest)的服务器推送框架,消息以JSON方式传递,具备长轮询、长连接、自动选择三种工作模式。 功能特性 推送消息广播。 推送定向消息。 提供连接上线前、上线...
pushlet简单demo,导入即可运行 Java 推送 长连接 轮询 pushlet comet4j
Comet4J(Comet for Java)是一个纯粹基于AJAX(XMLHTTPRequest)的服务器推送框架,消息以JSON方式传递,具备长轮询、长连接、自动选择三种工作模式。ojdbc7-12.1.0.2是 适用于jdk7,Oracle版本12的包。