Comet---"Server Push" Technology Implementation

Comet is a technology that pushes data from the server. It is based on long HTTP links to avoid resource overhead caused by too many links. For the intelligent monitoring system, it is a suitable technical choice. Because the monitoring system needs to push the updated information to the client in real time.

The following parts are quoted from the IBM official website:

About Comet

As the foreground of Web applications, browsers have limited processing capabilities. The development of browsers requires the client to upgrade software, and at the same time, due to the diversity of client browser software, in a sense, it also affects the promotion of new browser technologies. In a web application, the main job of the browser is to send requests and parse the information returned by the server to display them in different styles. AJAX is the result of the development of browser technology, which improves the responsiveness of single-user operations by sending asynchronous requests on the browser side. But the Web is essentially a multi- user system, and to any user, the server can be thought of as another user. The development of the existing AJAX technology cannot solve the problem that in a multi-user Web application, the updated information is transmitted to the client in real time, so that the user may operate under the "outdated" information. The application of AJAX makes it possible to update the background data more frequently.

Figure 1. Comparison of traditional web application model and AJAX -based model

" Server push " is a technology that has existed for a long time. In the past, it was mainly implemented through client sockets or remote calls on the server side. Because the development of browser technology is relatively slow, and there is no good support for the implementation of " server push " , it is difficult to have a complete solution to realize " server push " and use it in commercial programs in pure browser applications . In recent years, because of the popularization of AJAX technology, and embedding IFrame in the ActiveX component of "htmlfile" can solve the loading and display problem of IE , some popular applications such as meebo and gmail+gtalk have used these new technologies in their implementation; At the same time , " server push " does have many demands in real applications. For these reasons, pure browser-based " server push " technology has begun to receive more attention, which Alex Russell ( the Dojo Toolkit project Lead ) calledThe " server push " technology of HTTP long connection and no need to install plug-ins on the browser side is "Comet" . At present, some mature Comet applications and various open source frameworks have appeared; some Web servers such as Jetty have also made many improvements to support a large number of concurrent long connections. Please refer to the Comet wiki for the latest developments in Comet technology .

The following introduces the java implementation of comet:

Set TOMCAT7.0 and Eclipse have been installed,

  • Download comet4j-tomcat7.jar and comet4j.js and place them in the corresponding directory of the project
  • Modify the server.xml configuration of tomcat to use nio.

<Connector URIEncoding="UTF-8" connectionTimeout="20000"

port="8080" protocol="org.apache.coyote.http11.Http11NioProtocol" redirectPort="8443"/>

  • Configure the monitoring and comet connection address in the web.xml of the application project

<listener>

<listener-class>org.comet4j.core.CometAppListener</listener-class>

</listener>

<listener>

<description>ComnetListener</description>

<listener-class>comet.ComnetListener</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>

4. Server-side code writing, here we need to write a ServletContextListener, define the data channel, and pass the data.

In the example below, the system sets up a thread to read data from the memory structure every three seconds.

import java.util.ArrayList;

import javax.servlet.ServletContextEvent;

import javax.servlet.ServletContextListener;

import monitorlogic.MonitorUser;

import org.comet4j.core.CometContext;

import org.comet4j.core.CometEngine;

 

public class ComnetListener implements ServletContextListener {

      private static final String CHANNEL1 = "num";

public void contextInitialized(ServletContextEvent arg0) {

CometContext cc = CometContext.getInstance();

cc.registChannel(CHANNEL1);

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(3000);

}

catch (Exception ex)

{

ex.printStackTrace();

}

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

StringBuffer htmlResult = new StringBuffer();

       ArrayList<Integer> al = MonitorUser.getUserList();

           int sum = 0;

           for (int i=0;i<al.size();i++)

           {

                 sum = sum +al.get(i);

           }

           int span = al.size()+1;

                                 

           engine.sendToAll(CHANNEL1, sum);

}

}

}

public void contextDestroyed(ServletContextEvent arg0)

{

     

}

}

5. Writing web pages to display data

<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>

<!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>Insert title here</title>

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

<script type="text/javascript">

function init(){

var kbDom = document.getElementById('bb');

JS.Engine.on({

num : function(bb){// Listen to a channel

kbDom.innerHTML = bb;

}

      });

JS.Engine.start('conn');

JS.Engine.on(

'start',function(cId,channelList,engine){

alert(' The connection has been established, the connection ID is: ' + cId);

});

}

</script>

</head>

<body οnlοad="init()">

      <h2> Today's total: <FONT color=red id="bb"></FONT>pieces</h2>

</body>

</html>

 


If you agree with this article, please click on the QR code below to follow this subscription number. Long press the QR code in the picture, and then click "Identify the QR code in the picture".

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324489003&siteId=291194637