Simple implementation of ASP. Net-based comet

Source: Internet
Author: User

I have been diving for many years. Today I suddenly appeared. I haven't written a blog for a long time, not because I don't want to write it, but because I don't have any feelings and I don't know where to start.

I saw a blog post "build my webim step by step" the day before yesterday. I think the concept of comet is very interesting, but the bloggers use their own complicated framework and it looks quite difficult, therefore, I want to use the simple "jquery + two classes" to implement a simple comet model and add the source code.

As follows:

 

I. Concepts

The concept of comet is explained with a smile: "HTTP persistent connections that drag long tails like a comet ". As we all know, HTTP cannot be persistently connected to the server. If every request is persistently connected to the server, the server will be down for a long time, just as a few days ago, the number of connections in the blog Park has exceeded 2000, and the service unavailable has come out ..

As explained on the Internet:

Step 1. send a request to the server

Step 2. Check whether data exists on the server

Step 3. If there is data, send the data to the client, end the request, and return step 1.

Step 4. If no data exists, wait until data appears and enter step 3.

From the preceding steps, we can see that the client and the server maintain a connection. In this way, the client and the server always have a connection that is not closed. That is, what they call "HTTP persistent connection ".

 

Ii. Comet Implementation Based on ASP. NET and jquery

In fact, jquery is just a means. Here we use its Ajax part for sending requests to the server. In my source code, it is used to send two requests to the server: 1. Send data 2. Send a waiting request (when the server has data, the request will be returned and sent again)

On the server side, how can we determine whether the server can return data and wait without data? If ASP. NET implements asynchronous processing, you can implement the ihttpasynchandler interface. The beginprocessrequest (httpcontext context, asynccallback CB, object extradata) method of this interface returns an iasyncresult object. If the callback function of the CB parameter is not called, the request will not end.

So we can do it in this methodArticleThe message processing class messages implemented in the singleton mode saves the iasyncresult object of all requests, so that you can know how many clients have sent the request, you can also traverse all iasyncresult objects to actively send data to the client.

As mentioned above, jquery will send two requests, and the second is a "Waiting request". When the server actively sends data to the client, the request can receive the data sent by the server. In this way, the server actively sends data to the client. Re-establish the waiting request and repeat the loop to achieve "persistent connection ".

Iii. Source Code

I do not have a drawing tool locally. I cannot express the above process in a flowchart. Please use haihan.CodeIt is also very simple, that is, it cannot be simply implemented. If you are interested, download it and check it out:

/files/hanxianlong/comet_by_asp.net.rar

Related Article

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.