修改服务器配置文件
因为Comet4J工作在NIO方式下,所以我们需要调整服务器连接器配置,更换为NOI连接器。 打开server.xml文件将找到原先的连接器配置:
public class HelloWorld implements ServletContextListener{
public static HelloWorld helloWorld ;
private static CometEngine engine;
private static final Object synLock = new Object();
int i=0;
//单例
public static HelloWorld getInstance() {
if (helloWorld == null) {
synchronized (synLock) {
if (helloWorld == null) {
helloWorld = new HelloWorld();
engine =CometContext.getInstance().getEngine();
}
}
}
return helloWorld;
}
private static final String CHANNEL="socket";
private static final String CHANNELORDER="socket_for_order";
public void contextInitialized(ServletContextEvent arg0){
System.out.println("contex");
System.out.println("1111");
CometContext cc =CometContext.getInstance();
cc.registChannel(CHANNEL);//注册应用的channel
cc.registChannel(CHANNELORDER);//注册应用的channel
}
public void broadcastMsg(String agv){
if(helloWorld!=null){
agv= agv.replace("],[", ",");
JSONArray obj = JSONArray.fromObject(agv);
engine.sendToAll(CHANNEL,obj);
}
}
public void broadcastMsgforOrder(String agv){
if(helloWorld!=null){
agv= agv.replace("],[", ",");
JSONArray obj = JSONArray.fromObject(agv);
engine.sendToAll(CHANNELORDER,obj);
}
}
- <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"/>
客户端
我们利用Comet4J开发一个每隔一秒向所有客户端推送服务器的剩余内存大小。 helloworld.html- <!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-0.0.2.js"></script>
- <script type="text/javascript">
- function init(){
- var kbDom = document.getElementById('kb');
- JS.Engine.on({
- hello : function(kb){//侦听一个channel
- kbDom.innerHTML = kb;
- }
- });
- JS.Engine.start('conn');
- }
- </script>
- </head>
- <body onload="init()">
- 剩余内存:<span id="kb">...</span>KB
- </body>
- </html>
服务端
helloworld.javapublic class HelloWorld implements ServletContextListener{
public static HelloWorld helloWorld ;
private static CometEngine engine;
private static final Object synLock = new Object();
int i=0;
//单例
public static HelloWorld getInstance() {
if (helloWorld == null) {
synchronized (synLock) {
if (helloWorld == null) {
helloWorld = new HelloWorld();
engine =CometContext.getInstance().getEngine();
}
}
}
return helloWorld;
}
private static final String CHANNEL="socket";
private static final String CHANNELORDER="socket_for_order";
public void contextInitialized(ServletContextEvent arg0){
System.out.println("contex");
System.out.println("1111");
CometContext cc =CometContext.getInstance();
cc.registChannel(CHANNEL);//注册应用的channel
cc.registChannel(CHANNELORDER);//注册应用的channel
}
public void broadcastMsg(String agv){
if(helloWorld!=null){
agv= agv.replace("],[", ",");
JSONArray obj = JSONArray.fromObject(agv);
engine.sendToAll(CHANNEL,obj);
}
}
public void broadcastMsgforOrder(String agv){
if(helloWorld!=null){
agv= agv.replace("],[", ",");
JSONArray obj = JSONArray.fromObject(agv);
engine.sendToAll(CHANNELORDER,obj);
}
}
配置
web.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>/conn</url-pattern>
- </servlet-mapping>
- <listener>
- <description>HelloWorld</description>
- <listener-class>org.comet4j.demo.helloworld.HelloWorld</listener-class>
- </listener>