Comet comet (ii) message transmission based on server push

Source: Internet
Author: User
Tags silver light
Introduction to comet (I) server push Introduction:

In the previous article, I briefly introduced the comet mechanism and principle. There is indeed a lot of similar information available on the Internet. The framework based on PHP, JSP, And. NET is even more endless. This article briefly introduces and analyzes a. Net-based server push framework, and implements a series of server push key technologies, including message Publishing, broadcast, and receiving.

Key Technical Points of Comet:

The concept of comet is advanced and direct. It publishes information from the server and allows the browser (or client) to receive and respond to message events. This is a simple concept that causes multiple troubles in system design.

First, how can we make our browser receive events from the server?

One way is to indirectly connect to the client socket by using components such as flash, IFRAME, or applet without entity size, push information to these components, and use JavaScript threads, obtain the information returned by the component (of course, in some cases, these components directly inject information to the page ).

The final result shows that we have a stable connection to the server and refreshed the page information. But in fact, this implementation method is not "direct ".

Flash (or flex) itself can call WSDL through WebService components, or directly call Java serverlet, which is a convenient condition for Flash Player, but implementing server push through Flash Communication is always unacceptable. Why not directly implement Flash Communication.

Not to mention the applet, directly "push" the jar package to the page to run, start the troublesome JRE and download the huge JavaProgram. If you do not have a JRE, You need to download it.

The silver light mechanism is similar to flash, because Flash Player is not popular, users also need to download the corresponding player.

The above three are not completely Web applications. We just want to use the purest JavaScript and HTML to solve the problem. Why bother the public? Simply JavaScript events and threads cannot meet our needs?

As a result, some people think of IFRAME. They send Ajax requests through an invisible IFRAME and get messages through long polling. However, this is not really a comet. At most it is an immature Ajax application.

The essence of comet is to use the server and JavaScript to maintain the browser's persistent connection and complete the browser-side response to server-side events. Such an event broadcast mechanism is cross-network and real-time.

Technical difficulties of Comet:

1. Maintain persistent connections

2. Maintain the "Heartbeat" of the server"

3. browser-Side "Sensing" of "Heartbeat"

4. Maintain the balance between persistent connections and timeout

5. Thread Synchronization

6. General Interface Design

PS: we are talking about the comet technology, not how to find a girlfriend. Although the two seem to have many similarities from a certain point of view ......

. Net comet implementation:

We need comet, we need the source code, and we need the simplest implementation. Based on these three requirements, I have prepared the following example: James Simpson's. Net im Based On comet technology.

Figure 1.1 comet base class diagram

 

Entity class:

1. cometclient: the service entity of Comet. The significance of instantiating a cometclinet is to record necessary service information, such as user name timeout settings.

2. cometmessage: Contains entity classes for service information and trust attributes. It is the subject of message transmission.

3. inproccometstateprovider: inherits icometstateprovider and implements message operation methods. At the same time, the message is saved in the memory.

Interface Class:

1. icometstateprovider: defines a series of interfaces to provide operations on cometclient entities and cometmessage.

Exception type:

1. cometexception: there are no special exception classes.

Event:

1. cometclienteventhandler: event listening and broadcasting of cometclient.

Thread-related:

1. cometwaitthread: server-side thread pool, waiting in queue for information requests and calls, and providing timeout and other operations.

2. cometwaitrequest: a server-side thread that listens to client message requests.

3. cometasyncresult: The management class of asynchronous requests. It is implemented asynchronously by Inheriting System. iasyncresult.

Subject:

1. cometstatemanager: A factory class used to manage thread pools, management threads and user connections, message forwarding, and other operations.

PS: In a sense, the best part of this example is that its cometstatemanager implementation can be expanded to customize its own management mode, you can use this class to customize your own storage operations. Thread management is relatively mature in this demo, and the event mechanism can be more abundant by customizing the adapter.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.