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
- 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)
- {//dead Loop keep long link
- str = "<script >window.parent.change ('" + DateTime.Now.ToLongTimeString () + "') </script>";
- This. Context.Response.Write (str);
- This. Context.Response.Flush (); //Input script call out
- System.Threading.Thread.Sleep (1000);
- }
- }
- }
WebForm1.aspx
HTML code
- <! 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) {
- Window.document.getElementById ("Div1"). innertext=str;
- }
- function onload () {
- var ifrpush = new ActiveXObject ("Htmlfile");//Create Object
- Ifrpush.open (); Open it
- var ifrdiv = ifrpush.createelement ("div");//Add a div
- Ifrpush.appendchild (IFRDIV); Add to Htmlfile
- Ifrpush.parentwindow.change=change; Register JavaScript Method I don't understand why I have to register
- ifrdiv.innerhtml = "<iframe src= ' default.aspx ' ></iframe>";//Add IFrame in div
- Ifrpush.close (); Shut down
- }
- OnLoad ();
- </Script>
- </head>
- <body>
- <div style="Float:left"> Current time is:</div>
- <div id="Div1"></div>
- </body>
- </html>