欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Java--Comet之服务端消息推送

程序员文章站 2022-05-06 15:09:30
...

Comet是一种用于web推送技术,能使服务器能实时地将更新的信息传送到客户端,而无须客户端发出请求,目前有两种实现方式,长轮询和iframe流

一:所需资源

1.comet4j-tomcat7-v0.0.2.jar

2.servlet-api.jar 3.0

二:tomcat 配置(server.xml中启动nio)


<Connector protocol="org.apache.coyote.http11.Http11NioProtocol" connectionTimeout="20000" port="80"  redirectPort="8443"/>

三:监听器配置(web.xml)

<listener>
        <listener-class>org.comet4j.core.CometAppListener</listener-class>
    </listener>
    <servlet>
        <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>

四:服务端代码示例

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(), "Sender App Module");
                helloAppModule.setDaemon(true);
                helloAppModule.start();

        }

        class HelloAppModule implements Runnable {
                public void run() {
                        while (true) {
                                try {
                                        Thread.sleep(1000);
                                } catch (Exception ex) {
                                        ex.printStackTrace();
                                }
                                CometEngine engine = CometContext.getInstance().getEngine();
                                engine.sendToAll(CHANNEL, Runtime.getRuntime().freeMemory()/1024);
                        }
                }
        }

        public void contextDestroyed(ServletContextEvent arg0) {

        }
}

五:客户端代码示例

<!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(){
        alert(1);
        var kbDom = document.getElementById('kb');
        JS.Engine.on({
                hello : function(kb){//侦听一个channel
                        kbDom.innerHTML = kb;
                }
        });
        JS.Engine.start('conn');
        JS.Engine.on(
        'start',function(cId,channelList,engine){
              alert('连接已建立,连接ID为:' + cId);
        });
}
</script>
</head>
<body nload="init()">
        剩余内存:<span id="kb">...</span>KB
</body>
</html>

六:开源的Comet框架

1.jetty

2.pushlet