准备工作:
comet4j-tomcat7.jar 或者comet4j-tomcat76jar根据tomcat而定
comet4j.js
修改配置:
tomcat的server.xml中修改:
<Connector URIEncoding="UTF-8"
connectionTimeout="20000"
port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" redirectPort="8443"/>
xml中增加:
<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>/comet</url-pattern>
</servlet-mapping>
<listener>
<description>TestComet</description>
<listener-class>com.richsum.xnoa.common.TestComet</listener-class>
</listener>
Java类中:
package com.richsum.xnoa.common;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import org.comet4j.core.CometContext;
import org.comet4j.core.CometEngine;
public class TestComet implements ServletContextListener{
private static final String CHANNEL = "test";
private static final String CHANNEL2 = "test2";
public void contextDestroyed(ServletContextEvent arg0) {
}
public void contextInitialized(ServletContextEvent arg0) {
CometContext cc = CometContext.getInstance();
cc.registChannel(CHANNEL);// 注册应用的channel
cc.registChannel(CHANNEL2);
Thread helloAppModule = new Thread(new HelloAppModule(), "Sender App Module");
// 是否启动
helloAppModule.setDaemon(true);
// 启动线程
helloAppModule.start();
Thread helloAppModule2 = new Thread(new HelloAppModule2(),"Sender App Module");
// 是否启动
helloAppModule2.setDaemon(true);
// 启动线程
helloAppModule2.start();
}
class HelloAppModule2 implements Runnable {
public void run() {
while (true) {
try {
Thread.sleep(2000);
}catch (Exception ex) {
ex.printStackTrace();
}
CometEngine engine = CometContext.getInstance().getEngine();
long l = getFreeMemory();
engine.sendToAll(CHANNEL2, l);
}
}
}
class HelloAppModule implements Runnable {
public void run() {
while (true) {
try {
Thread.sleep(2000);
}catch (Exception ex) {
ex.printStackTrace();
}
CometEngine engine = CometContext.getInstance().getEngine();
long l = getFreeMemory();
engine.sendToAll(CHANNEL, l);
}
}
}
public long getFreeMemory() {
return Runtime.getRuntime().freeMemory() / 1024;
}
}
jsp中:
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@ taglib uri="/struts-tags" prefix="s"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
request.setAttribute("path",path);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Comet4J Hello World</title>
<script type="text/javascript" src="js/comet4j.js"></script>
<script type="text/javascript">
function init(){
var kbDom = document.getElementById('kb');
var kbDom2 = document.getElementById('kb2');
JS.Engine.start('comet');
JS.Engine.on({
test : function(aa){//侦听一个channel
kbDom.innerHTML = aa;
},
test2 : function(bb){
kbDom2.innerHTML = bb;
}
});
}
</script>
</head>
<body οnlοad="init()">
剩余内存:<span id="kb">...</span>KB <br/>
剩余内存:<span id="kb2">...</span>KB
</body>
</html>