ASP. Server Push Technology (server push)

Source: Internet
Author: User

In the past and server-side communication technology, most of US use Ajax polling access, that is, control the time interval in JavaScript, and then access the server every once in a while, and then get data or notifications. But 90% of these polling methods are doing a useless job.
What if I want to keep the connection between the server and the browser for a long time? Long connection technology, this is not a new technology, using IFRAME as a hidden frame to point to the long Request page method has been used by many people on the internet, but the IFRAME as a hidden frame has a disadvantage, that is, the browser's progress bar is always in the read state. To give users a better experience, "Google's geniuses" used an object called "Htmlfile" to solve the problem and apply it to Gmail and gtalk two of products.
Now we want to do new projects on the need for real-time alarm function, I would like to use Ajax polling do, but feel very not pursued, some time ago heard that the server Push, but did not study carefully, this is a chance, a day, from the Internet to collect information. The information is not many, and now a lot of developers also think long connection is the Arabian Nights, incredibly still have the HTTP protocol out to prove their point of view ...
Not much nonsense to say, to introduce a long connection technology, the usual long link is to make a Web page, which is written in an IFRAME tag, aspect set to the 0,SRC attribute point to a page, such as ASPX, and then in this file do not do anything else, Just call the Context.Response.Write method and output what? For example, if the client has a change time method, the output is ("<script>window.parent.change (" +datetime.now.tostring () + ") </script > "), that is, the continuous output of the client's function calls, and into a dead loop, so that the browser and server side formed a continuous stream of data transmission links.
What is that htmlfile? This is a activexobject similar to the Window object in JavaScript, it is also the DOM structure, which will be written as a hidden frame of the IFrame to the object, you can solve the problem of the progress bar. May be more obscure, look at the example code:

Default.aspx.cs

C # code
  1. Public partial class _default:system.web.ui.page
  2. {
  3. protected void Page_Load (object sender, EventArgs e)
  4. {
  5. }
  6. Protected override void Render (HtmlTextWriter output)
  7. {
  8. String str;
  9. while (true)
  10. {//dead Loop keep long link
  11. str = "<script >window.parent.change ('" + DateTime.Now.ToLongTimeString () + "') </script>";
  12. This. Context.Response.Write (str);
  13. This. Context.Response.Flush (); //Input script call out
  14. System.Threading.Thread.Sleep (1000);
  15. }
  16. }
  17. }




WebForm1.aspx

HTML code
    1. <! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">>
    2. <HTML xmlns="http://www.w3.org/1999/xhtml">
    3. <head runat="Server">
    4. <title>asp.net Server Push</title>
    5. <script type="Text/javascript">
    6. function Change (str) {
    7. Window.document.getElementById ("Div1"). innertext=str;
    8. }
    9. function onload () {
    10. var ifrpush = new ActiveXObject ("Htmlfile");//Create Object
    11. Ifrpush.open (); Open it
    12. var ifrdiv = ifrpush.createelement ("div");//Add a div
    13. Ifrpush.appendchild (IFRDIV); Add to Htmlfile
    14. Ifrpush.parentwindow.change=change; Register JavaScript Method I don't understand why I have to register
    15. ifrdiv.innerhtml = "<iframe src= ' default.aspx ' ></iframe>";//Add IFrame in div
    16. Ifrpush.close (); Shut down
    17. }
    18. OnLoad ();
    19. </Script>
    20. </head>
    21. <body>
    22. <div style="Float:left"> Current time is:</div>
    23. <div id="Div1"></div>
    24. </body>
    25. </html>  

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.