Simple implementation of comet based on ASP.

Source: Internet
Author: User

Http://www.cnblogs.com/hanxianlong/archive/2010/04/27/1722018.html

I have been diving for many years and I suddenly appear today. Haven't written a blog for a long time, not because do not want to write, but because there is no feeling, do not know where to write.

The day before yesterday to see a blog "Step by step to create their own Webim", think comet this concept is very interesting, but bloggers use their own set of more complex framework, looks rather laborious, so I want to through a simple "jquery+ two classes" to achieve a simple comet model, and the source code is attached , please shoot the bricks.

As follows:

First, the concept

Borrow the cross-knife to explain this comet concept: "Like a comet dragging long tail of the long-term HTTP connection." In fact, we all know that HTTP is not a persistent connection with the server, if each request is persistent connection with the server, the server has been down, just like the previous period of time like a blog garden connection more than 2000 out of the service unavailable.

Just like the online explanation:

Step 1. Making a request to the server

Step 2: Server-side view for data

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

Step 4. If there is no data, wait until there is data to appear, enter step 3

As you can see from the above steps, the client and server always maintain a connection, so that it looks like there is always a connection between the client and the server that is not closed. That's what they call an "HTTP long connection."

Second, the comet implementation based on ASP and jquery

In fact, jquery is just a means of using its AJAX part to send requests to the server. In my source code, is to be responsible for sending two requests to the server side: 1, send data 2, send wait request (when the server has data, the request will return and send again)

As for the server side, how to determine the server has data to return, no data to wait for the function? Google, you can know that ASP. NET implementation of asynchronous processing, the implementation of the IHttpAsyncHandler interface can be. The interface's BeginProcessRequest (HttpContext context, AsyncCallback cb, Object Extradata) method returns a IAsyncResult object. When the callback function for its parameter CB is not called, the request does not end.

Therefore, we can do the article in this method, with a singleton pattern implementation of the message processing class messages all the requested IAsyncResult object is saved, so that you can know how many clients send the request, but also can traverse all the IAsyncResult object, Implements the ability to proactively send data to its clients.

It says that jquery sends two requests, and the second one is a "wait request", which can receive the data sent by the server when the server is actively sending the data to the client. This completes the server to proactively send data to the client. Create a wait request again, repeat the loop, and implement a "long connection".

Third, the source code

I do not have a local drawing tools, can not be used in the process flow diagram is very good to express, please Haihan. Code is also very simple, is a simple can not be easily implemented, interested in downloading down to see:

/files/hanxianlong/comet_by_asp.net.rar

Simple implementation of comet based on ASP.

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.