Legendary push technology

Source: Internet
Author: User

In the past communication technologies with servers, we used Ajax round-robin access, that is, controlling the time interval in Javascript, and then accessing the server at intervals, then obtain the data or notification. However, 90% of the requests in this round robin method are useless.

What should I do if I want to maintain the connection between the server and the browser for a long time? Persistent connection technology is not a new technology. Using IFRAME as a method to hide frames pointing to long request pages has already been used by many people on the Internet. However, IFRAME as a hidden frame has a drawback, that is, the progress bar of the browser is always in the Read state. To provide a better user experience, "Google talents" used an object named "htmlfile" to solve this problem and applied it to two products, Gmail and Gtalk.

Now our company needs to implement real-time alarm functions for new projects. I originally wanted to use Ajax Round Robin, but I felt quite unpursued. I heard that server push was available some time ago, but I did not study it carefully. This is a chance to collect data from the Internet in one day. There are not a lot of materials, and many developers still think that persistent connections are a fantasy, and they still need to prove their point of view by moving the HTTP protocol out ......

Let's talk about the persistent connection technology. The common persistent link is to create a webpage, which has an IFRAME tag, and the height and width are set to 0. The src attribute points to a webpage, for example, it is aspx, And then it does not do anything in this file, just calling context. response. write method. What is output? For example, if the client has a change (time) method, the output is ("<SCRIPT> window. parent. change ("+ datetime. now. tostring () + ") </SCRIPT>"), that is, the function calls of the client are continuously output and made into an endless loop, in this way, the browser and the server form a continuous stream of data transmission links.

What is htmlfile? This is an activexobject similar to the window object in Javascript. It is also in the DOM structure and writes the IFRAME as the hidden frame to this object to solve the progress bar problem. It may be obscure. Let's look at the example.CodeRight:

Default. aspx. CS

C # code

Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{

}

Protected override void render (htmltextwriter output)
{

String STR;
While (true)
{// Keep a persistent link in an endless loop
STR = "<SCRIPT> window. Parent. Change ('" + datetime. Now. tolongtimestring () + "') </SCRIPT> ";
This. Context. response. Write (STR );
This. Context. response. Flush (); // input the script to call
System. Threading. thread. Sleep (1000 );
}
}
}
Webform1.aspx

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> Asp.net server push </title>

<SCRIPT type = "text/JavaScript">
Function Change (STR ){
Optional bytes Doc ument. getelementbyid ("div1"). innertext = STR;
}
Function onload (){
VaR ifrpush = new activexobject ("htmlfile"); // create an object
Ifrpush. open (); // open
VaR ifrdiv = ifrpush. createelement ("Div"); // Add a div
Ifrpush. appendchild (ifrdiv); // Add to htmlfile
Ifrpush. parentwindow. Change = change; // The javascript registration method does not understand why you need to register
Ifrdiv. innerhtml = "<IFRAME src = 'default. aspx '> </iframe>"; // Add IFRAME to the DIV
Ifrpush. Close (); // close
}

Onload ();
</SCRIPT>
</Head>
<Body>
<Div style = "float: Left"> current time: </div>
<Div id = "div1"> </div>
</Body>
</Html>

Default. aspx does not need to be modified.

 

 

Implementing persistent connections in IE still relies on the idea of Google's cool-man, and uses activexobject ("htmlfile") to push the server to the client...

To implement persistent connections in Firefox, Ajax is required. The push effect can be achieved by using the readystate attribute of the XMLHttpRequest () object instance, long link can be implemented on the client as long as readystate = 3 is used as the judgment condition...It took me a day to implement this function, but this implementation method is obviously less efficient than activexobject ("htmlfile") in IE, but I have no other way, I had to use it first, and wait for a better way to replace it later ~ Haha, thank you for helping me pin the post. Haha, let me put it for another day. I will close the post tomorrow ~ I hope that friends who are equally confused will be inspired by my method ~

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.