Example of HTTP server push under ASP. NET

Source: Internet
Author: User

By default, HTTP is a back-and-forth request, and the response is returned, even if it is over. This greatly improves the utilization efficiency of web servers. Don't be stuck at the door.

 

However, in some cases, some people may want to hide them on the page. For example, stock quotations and online chat, after all, it is not good for people to check the stock every few seconds. It is best to notify the browser immediately when the data changes.

 

This technology uses server push. Http://en.wikipedia.org/wiki/Push_technology (Glossary)

 

After looking for a long time, I did not find a suitable example. I found one but couldn't be better at it. Then I changed it and ran very well. The principle is quite simple. Write it down for reference. This is the principle of Dynamic Refresh of Yahoo's stock market.

 

 

Code
Protected   Override   Void Render (htmltextwriter output)
{
Base . Render (output );

Response. Buffer= True;
BoolIsoutput= False;
Response. Write ("");

Response. Flush ();
Int Lastsecond =   0 ;
While (Response. isclientconnected)
{
Thread. Sleep ( 300 );
If (Datetime. Now. Second ! = Lastsecond &&   ! Isoutput)
{

Lastsecond = Datetime. Now. Second;
Response. Write ( " <SCRIPT> settimer (' "   + Datetime. Now. tostring () +   " ') \ N </SCRIPT> " );
Response. Flush ();
Isoutput =   True ;
}
Else
{
Isoutput =   False ;
}

}

 

 

 

 

Related Article

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.