Comet4J 相关

来源:互联网 发布:linux ssh ip port 编辑:程序博客网 时间:2024/05/21 22:29

参考:http://www.zuidaima.com/share/1948215561653248.htm

Comet4J(Comet for Java)是一个纯粹基于AJAX(XMLHTTPRequest)的服务器推送框架,消息以JSON方式传递,具备长轮询、长连接、自动选择三种工作模式。

Comet4J目前仅支持Tomcat6、7版本,根据您所使用的Tomcat版本下载【comet4j-tomcat6.jar】或【comet4j-tomcat7.jar】文件放置到WEB项目的WEB-INF\lib目录下。

修改:因为Comet4J工作在NIO方式下,所以我们需要调整服务器连接器配置,更换为NOI连接器。 修改tomcat的server.xml

 <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"/> 


具体操作如下:

一.配置
1.引用comet4j-tomcat7 jar 包
2.web.xml中增加配置
<!--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>

<!--comet4j应用配置-->
<listener>
<description>描述</description>
<listener-class>com.comet4j.AppInit</listener-class>
</listener>




二.html 页面


1.引入comet4j.js
<script type="text/javascript" src="js/comet4j.js"></script>


2.增加js
$(function() {
initEngine();//初始化comet4j引擎 
xxxx();//其他方法
}
//初始化服务器推监听 此方法写在页面加载事件的最前面,放后面会被其他方法影响导致不能正常运行
function initEngine(){
// 引擎事件绑定
JS.Engine.on({
start : function(cId, aml, engine) {},
stop : function(cause, url, cId, engine) {},
comet4jTongDao: function(data, timespan, engine) {//comet4jTongDao 这个名字是自己起的,但是要和后台的 registChannel 对应的通道名字一样
//处理自己的事情
}
});
JS.Engine.start('conn');
}


3.对应的报价 或者聊天 js改写如下


JS.AJAX.post(url, param, function(data) {xxxx});


三.后台


对应的方法 


    private static final CometContext context = CometContext.getInstance();
    private static final CometEngine engine = context.getEngine();

String id = request.getParameter("id");

MyDTO dto=MyDTO(id); //自定义一个实体类,包含有id这个字段即可。
 engine.sendToAll(Constant.APP_CHANNEL, dto);//给所有人发
engine.sendTo(Constant.APP_CHANNEL, engine.getConnection(id),dto);//异常只给单个人发


其他的支持类如下:
public class AppInit implements ServletContextListener {


/**
* 用于初始化信息
*/
@SuppressWarnings("unchecked")
public void contextInitialized(ServletContextEvent event) {


CometContext cc = CometContext.getInstance();
CometEngine engine = cc.getEngine();


//绑定事件 侦听 上下线
engine.addConnectListener(new OnLineListener());
engine.addListener(DropEvent.class, new OffLineListener());


cc.registChannel(Constant.APP_CHANNEL);// 注册通道


// 启动系统监控信息发送器
Thread healthSender = new Thread(new JianKongSender(), "JianKongSender");
healthSender.setDaemon(true);
healthSender.start();
}


public void contextDestroyed(ServletContextEvent arg0) {
}


}




public class Constant {
// 通道标识
public static final String APP_CHANNEL = "comet4jTongDao";// 这个必须和js方法中的 JS.Engine.on 里面的方法对应。这个方法名字可以自己起
}


public class JianKongSender implements Runnable {


        private static final CometEngine engine = CometContext.getInstance().getEngine();


        public void run() {
               /* while (true) {
                        try {
                                Thread.sleep(500);
                        } catch (Exception ex) {
                                ex.printStackTrace();
                        }
                        long totalMemory = Runtime.getRuntime().totalMemory();
                        long freeMemory = Runtime.getRuntime().freeMemory();
                        long maxMemory = Runtime.getRuntime().maxMemory();
                        long usedMemory = totalMemory - freeMemory;
                        Integer connectorCount = engine.getConnections().size();
                        
                        engine.sendToAll(Constant.APP_CHANNEL, xxx);


                }*/
        }
}




public class OffLineListener extends DropListener {


public boolean handleEvent(DropEvent event) {
CometConnection conn = event.getConn();
if (conn != null) {
//event.getTarget().sendToAll(Constant.APP_CHANNEL, xxx);
}
return true;
}


}


public class OnLineListener extends ConnectListener {


@Override
public boolean handleEvent(ConnectEvent anEvent) {
CometConnection conn = anEvent.getConn();
HttpServletRequest request = conn.getRequest();


//anEvent.getTarget().sendToAll(Constant.APP_CHANNEL, xxx);
return true;
}
}








0 0
原创粉丝点击