Cainiao comet + jquery completes the simple webim project debugging process!

Source: Internet
Author: User

The project writes an htpphander <add Path = "comet_broadcast.asyn" type = "asnyhandler" verb = "post, get"/>

In jquery's doucment, write a method:

$ (Document). Ready (function (){

Function send (){

// Send a request to comet_broadcast.asyn. The message body is the content in the text box content, and the request receiving Class is asnyhandler.

$. Post ("comet_broadcast.asyn", {content: $ ("# Content"). Val ()});

 

// Clear the content

$ ("# Content"). Val ("");

}

In this way, the comet_broadcast.asyn accessed by jquery can be called asnyhandler. second, the asnyhandler class implements the ihttpasynchandler interface, which can be called asynchronously by the Asp.net program. how can we keep a connection on the server and the client? 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. The program first enters the beginprocessrequest () method; beginprocessrequest method explanation: beginprocessrequest method return iasyncresult object this object is IasyncresultInterfaces are implemented by classes that contain methods that can be operated asynchronously. It is the return type implementation object of the method for starting asynchronous operations: myasynresult-> myasynresult asyncresult = new myasynresult (context, CB, extradata); there is an asynchronous Processing Method in myasynresult. [of course, The myasynresult class must be a realistic interface: iasyncresult] string content = context. request. params ["content"] receives parameters from jquery: Content string content = context. request. params ["content"]; // content =-1 when the object is run for the first time // The status indicates that no information is waiting for information. next: add the information to the message. // Add the message to the Message class. instance (). addme Ssage (content, asyncresult); Detailed description of the instance method: Private Static readonly messages _ instance = new messages (); // Why is the above static and read-only? This is a simple implementation method of the C # Singleton mode. this ensures that only one object is used when the _ instance class object is used. public static messages instance () {return _ instance;} // the above method is to give the message object to the call method. addmessage method details: // call the method in if when loading this page. public void addmessage (string content, myasynresult asyncresult) {// when the input content is "-1", it indicates a connection request, that is, to maintain a connection established from the client to the server // Save the connection to list <myasynresult> clients. When a message is sent, // The connection will be traversed and the connection output content will end the connection if (content = "-1") {clients. add (Asyncresult);} else {// output the content of the current request to the client asyncresult. content = content; // etc. The send method asyncresult will be introduced. send (null); // otherwise, All cached clients are traversed and the current content is output to the client foreach (myasynresult result in clients) {// The content object is the string object declared by the myasynresult object to broadcast the content. result. content = content; result. send (null);} // clear all cached clients. clear (); // clear the list object after broadcasting}. This completes the first operation. when you enter text in the broadcast and click to send it: the process still passes the web. confing to the asnyhandler class, execute beginpro Cessrequest method. instantiate Class Object: myasynresult receives the string content object through the content that jquery will broadcast. the above is the same as the first run until the addmessage method enters the else send method details: Public void send (Object Data) {context. response. write (this. content); If (CB! = NULL) {CB (this);} _ iscompleted = true;} The send method is actually very simple: the obtained content object is returned to jquery using jquery to operate in jquery. the content CB (this) method actually ends the request. this completes a complete request. why is it jumping to the myasynresult class? Check that the wait () method has been executed in jquery to connect the client to the server in such a loop. In this way, comet is implemented. I am a cainiao. This is the process of debugging the project. The source code is in: Prepare.

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.