标签:reg 目的 并且 接收 守护 zed ... add 版本
整个例子的源码下载:http://pan.baidu.com/s/1gfFYSbp
Comet4J目前仅支持Tomcat6、7版本,根据您所使用的Tomcat版本下载【comet4jtomcat6.jar】或【comet4j-tomcat7.jar】文件放置到WEB项目的WEB-INF\lib目录下。
本文下载comet4j-tomcat7.jar, 下载地址:http://pan.baidu.com/s/1eSBXmfS
<Connector executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />
替换为
<Connector URIEncoding="UTF-8" connectionTimeout="20000" port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" redirectPort="8443"/>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Cometd首页</title> <script type="text/javascript" src="./JS/cometd/comet4j.js"></script> <script type="text/javascript"> var projectPath = ‘<%= request.getContextPath()%>‘; function cometdCallback(text){ var kbDom = document.getElementById(‘num1‘); kbDom.innerHTML = text; } function init() { JS.Engine.on({ number : cometdCallback }); JS.Engine.start(projectPath+‘/conn‘); } </script> </head> <body onload="init()"> 数字3: <span id="num1">...</span> </body> </html>
其中,<%=request.getContextPath()%>是为了解决相对路径的问题,可返回站点的根路径。
package com.cvicse.ump.cometd; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener; import org.comet4j.core.CometContext; public class CometdTestClass implements ServletContextListener { private static final String CHANNEL_NUM = "number"; private static int number = 0; @Override public void contextDestroyed(ServletContextEvent arg0) { } @Override public void contextInitialized(ServletContextEvent arg0) { CometContext.getInstance().registChannel(CHANNEL_NUM); Thread timeThread = new Thread(new TimeThread(),"numTimeThread"); timeThread.setDaemon(true);//设置为守护线程 timeThread.start(); } class TimeThread implements Runnable{ @Override public void run() { while(true){ CometContext.getInstance().getEngine().sendToAll(CHANNEL_NUM, number++); System.out.println(CHANNEL_NUM+": "+number); try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } } } } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>cometd</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <listener> <description>Comet4J容器侦听</description> <listener-class>org.comet4j.core.CometAppListener</listener-class> </listener> <listener> <listener-class>com.cvicse.ump.cometd.CometdTestClass</listener-class> </listener> <servlet> <description>Comet连接[默认:org.comet4j.core.CometServlet]</description> <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> </web-app>
客户端是一个JavaScript文件(comet4j-0.0.2.js),其中最重要的是JS.Connector和JS.Engine两个类。JS.Connector负责与服务器建立并保持连接,而JS.Engine类负责将服务器推送过来的消息转化为开发人员可以处理的消息事件,并分发出去,大多数情况下,我们仅需要使用JS.Engine类就可以完成多数的开发工作。
JS.Engine类是一个静态类,在一个页面中只有一个JS.Engine类的实例。它除了负责把服务器推过来的消息转化为事件分发以外,与服务器的连接与断开也由此类负责。
JS.Engine.start(String str)和JS.Engine.stop(String str)分别控制连接和断开动作,start方法需要传入一个字符串参数,用来指定您配置的Comet4J连接地址。比如按前面准备工作的配置了CometServlet的地址为/conn,那么可以这样写:
JS.Engine.start(‘/conn‘);
JS.Engine.stop(‘主动断开‘);
上面代码我们让连接断开,并传入了一个“主动断开”这样一个断开的原因。如果您并不需要对断开的原因进行说明,也可以不传递参数:
JS.Engine.stop();
JS.Engine.on(‘start‘,function(cId, channelList, engine){ alert(‘连接已建立,连接ID为:‘ + cId); }); JS.Engine.on(‘stop‘,function(cause, cId, url, engine){ alert(‘连接已断开,连接ID为:‘ + cId + ‘,断开原因:‘ + cause + ‘,断开的连接地址:‘+ url); });
也可以将上段代码写成,下面代码与上段代码完全等效:
JS.Engine.on({ start : function(cId, channelList, engine){ alert(‘连接已建立,连接ID为:‘ + cId); }, stop : function(cause, cId, url, engine){ alert(‘连接已断开,连接ID为:‘ + cId + ‘,断开原因:‘ + cause + ‘,断开的连接地址:‘+ url); } });
接下来,介绍一下如何对服务器推送过来的消息进行处理。在介绍之前,我们假设后台已经注册了一个"hello"的应用通道标识,并且只向客户端推送简单的字符串信息。先看如下代码:
JS.Engine.on(‘hello‘,function(text){ alert(text); });
CometContext.getInstance().registChannel("hello");
ring channel = "hello"; String someConnectionId = "1125-6634-888"; engine.sendToAll(channel , "我来了!"); engine.sendTo(channel , engine.getConnection(someConnectionId),“Hi,我是XXX”);
上面代码使用sendToAll方法向所有客户端在"hello"通道上发送了“我来了!”这样一条消息,然后又使用sendTo在同样的通道上向某一个连接发送了“Hi,我是XXX”消息。 CometEngine另外一个很重要的地方在于,它是框架工作的事件引擎的集散地,它提供了BeforeConnectEvent、BeforeDropEvent、ConnectEvent、DropEvent、MessageEvent等事件。通过对这些事件的处理来实现具体的功能:
class JoinListener extends ConnectListener { @Override public boolean handleEvent(ConnectEvent anEvent) { CometConnection conn = anEvent.getConn(); CometContext.getInstance().getEngine().sendTo("hello", conn.getId(),"欢迎上线"); } } CometEngine engine = CometContext.getInstance().getEngine(); engine.addConnectListener(new JoinListener()
<!--Comet4J配置 --> <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> <!-- Comet4J可选参数配置--> <context-param> <description>语言[支持:zh,en,默认:zh,详细http://www.loc.gov/standards/iso639-2/php/English_list.php]</description> <param-name>Comet.Language</param-name> <param-value>zh</param-value> </context-param> <context-param> <description>请求超时时间/微妙[默认:60000,1分钟,建议至少设置3秒以上]</description> <param-name>Comet.Timeout</param-name> <param-value>60000</param-value> </context-param> <context-param> <description>连接空闲过期时间/微妙[默认:5000,5秒]</description> <param-name>Comet.ConnExpires</param-name> <param-value>5000</param-value> </context-param> <context-param> <description>连接检查频率/微妙[默认:5000,5秒]</description> <param-name>Comet.ConnFrequency</param-name> <param-value>5000</param-value> </context-param> <context-param> <description>缓存信息过期时间/微妙[默认:60000,1分种]</description> <param-name>Comet.CacheExpires</param-name> <param-value>60000</param-value> </context-param> <context-param> <description>缓存信息过期检查频率/微妙[默认:60000,1分种]</description> <param-name>Comet.CacheFrequency</param-name> <param-value>60000</param-value> </context-param> <context-param> <description>连接模式[auto(默认)/stream/lpool]</description> <param-name>Comet.WorkStyle</param-name> <param-value>auto</param-value> </context-param> <context-param> <description>开启调试[false(默认)/true]</description> <param-name>Comet.Debug</param-name> <param-value>false</param-value> </context-param>
标签:reg 目的 并且 接收 守护 zed ... add 版本
原文地址:http://www.cnblogs.com/dyh004/p/6708389.html