asp.net (server Push) _ Practical Tips

Source: Internet
Author: User
What if you 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 point to the Long Request page method has long 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 reading state. To give users a better experience, "Google's geniuses" used an object called "Htmlfile" to solve the problem, using Gmail and gtalk two products.
Now our company to do a new project on the demand for real-time alarm function, I would like to use Ajax polling to do, but feel very not pursuing, some time ago heard of the server Push, but did not study carefully, this is a chance, one day time, from the Internet to collect information. There is not a lot of data, and now many developers think that long connection is impossible, incredibly still have to move out the HTTP protocol to prove their point of view ...
Needless to say, to introduce the long connection technology, the usual long link is to do a Web page, which wrote an IFRAME tag, width set to 0,SRC attribute to a Web page, such as ASPX, and then in this file does not do anything else, Just call the Context.Response.Write method, output what? For example, the client has a method of changing the time, the output is ("<script>window.parent.change" ("+datetime.now.tostring () +") </script > "), that is, the constant output of the client's function calls, and make a dead loop, so that the browser and server-side formed a continuous stream of data transmission links.
What's the Htmlfile? This is a activexobject similar to a JavaScript window object, which is also a DOM structure that writes the IFRAME as a hidden frame to the object, and resolves the problem of the progress bar. Say may be more obscure, look at the example code bar:
Default.aspx.cs
C # code
Copy Code code as follows:

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 ();//The output script calls out
System.Threading.Thread.Sleep (1000);
}
}
}

WebForm1.aspx
Copy Code code as follows:

<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">
<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"); Creating objects
Ifrpush.open (); Open it
var ifrdiv = ifrpush.createelement ("div"); Add a div
Ifrpush.appendchild (IFRDIV); Add to Htmlfile
Ifrpush.parentwindow.change=change; Registering JavaScript methods I don't understand why I'm registering.
ifrdiv.innerhtml = "<iframe src= ' default.aspx ' ></iframe>"; Add iframe to Div
Ifrpush.close (); Shut down
}
OnLoad ();
</script>
<body>
<div style= "Float:left" > Now time is:</div>
<div id= "Div1" ></div>
</body>

Default.aspx do not need to be modified.
Serverpush.rar

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.