Using response. Flush and IFRAME to implement "server push" Technology

Source: Internet
Author: User
By hiding an IFRAME in the HTML page, set the src attribute of the IFRAME to a persistent connection request (using chunked to transmit response ), the server can continuously push data to the customer.

Stream-based "server push" Model:

When the server receives a request from the client, it sends data through the flush () method of response. You can use the timer to send data. If there is no data, it also sends "no data ", keep the client persistent connection until the client is disconnected and the request ends. The connection will not be closed during each data transmission, but will only be closed when communication errors occur or when the connection is rebuilt (Some firewalls are often set to discard too long connections, you can set a timeout time on the server to notify the client to re-establish the connection and close the original connection after the timeout ).

ImplementationCode:

The default. aspx page is used to display data:

 
1:Data list: O<BR/>
2:<DivID= "Con"Style="Width: 400;Height: 200px;Border: 1px solid # ff0 ">
 
3:</Div>
4:<IFRAMEID= "Flush"SRC= "Flush. aspx"Style="Display: None "/>
 
5:

The Flash. aspx background code corresponding to the SRC of ifame, simulating sending data in the background:

1:Protected voidPage_load (ObjectSender,EventargsE)
 
2:{
 
3:StringStarthtml ="<! Doctype HTML public \ "-// W3C // dtd xhtml 1.0 transitional // en \" \ "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd\">"+Environment. Newline
 
4:+"<HTML xmlns = \" http://www.w3.org/5o/xhtml\ ">"+Environment. Newline
5:+"<Head>"+Environment. Newline
 
6:+"</Head>"+Environment. Newline
 
7:+"<Body>"+Environment. Newline;
 
8: 
 
9:Starthtml + =NewString('', 1024) +Environment. Newline;
 
10: 
11:Response. Write (starthtml );
 
12:Response. Flush ();
 
13: 
 
14:StringData ="<SCRIPT type = \" text/JavaScript \ "> parent. $ ('# con'). append (\" {0} \ "); </SCRIPT>";
 
15:Response. Write (String. Format (data,"Start to send data: <br/>"));
 
16:Response. Flush ();
 
17: 
 
18:IntIndex = 0;
19:While(True)
 
20:{
 
21:System. threading.Thread. Sleep (2000 );
 
22:If(Index % 2 = 0)
 
23:{
 
24:Response. Write (String. Format (data,Datetime. Now. tostring ("Yyyy-mm-dd hh: mm: SS") +"Data sent by the server <br/>"));
 
25:}
26:Else
 
27:{
 
28:Response. Write (String. Format (data,Datetime. Now. tostring ("Yyyy-mm-dd hh: mm: SS") +"No data transmission <br/>"));
 
29:}
 
30:Response. Flush ();
 
31: 
 
32:Index ++;
 
33:}
 
34:}
35: 

Result of running default. aspx:

Using IFRAME to request a persistent connection has an obvious deficiency: The progress bar at the bottom of IE and morzilla Firefox will show that the loading is not completed, and the icons above IE will keep turning, loading is in progress. refreshing the current page will slow down.

The progress bar of IE shows that the loading is not complete. You can use an ActiveX called "htmlfile", which is used by Google talents. This control is also used in Gmail + Gtalk products.

Modify the Page code of default. aspx:

 
1:Y data column y d table í: O<BR/>
2:<DivID= "Con"Style="Width: 400;Height: 200px;Border: 1px solid # ff0 ">
 
3:</Div>
 
4:<ScriptType= "Text/JavaScript">
5:FunctionGetdata (d)
 
6:{
 
7:$ ("# Con"). Append (d );
 
8:}
 
9: 
 
10:FunctionRpc_iframe (){
 
11:VaRTransferdoc =NewActivexobject ("Htmlfile");
 
12:Transferdoc. open ();
13:Transferdoc. Write ("<HTML>")
 
14:Transferdoc. Write ("<Div> <IFRAME src = \" Flush. aspx \ "> </iframe> </div>");
 
15:Transferdoc. Close ("</Html>");
 
16:Transferdoc. parentwindow. getdata = getdata;
 
17:Setinterval (Function() {}, 10000 );// If this clause is not added, the connection will be disconnected.
 
18:}
 
19: 
20:Rpc_iframe ();
 
21:</Script>
 
22:

Modify the flush. aspx. CS code:

 
1:// String data = "<SCRIPT type = \" text/JavaScript \ "> parent. $ ('# Con '). append (\ "{0} \"); </SCRIPT> ";
2:StringData ="<SCRIPT type = \" text/JavaScript \ "> parent. getdata (\" {0} \ "); </SCRIPT>";
 
3: 

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.