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

Source: Internet
Author: User
In the previous article, I used IFRAME to implement "server push". This method has the disadvantage that IE and Firefox will see the progress bar loading status. In the previous article, I also introduced the solution to IE, we recommend that you first read the previous article. The progress bar is always loaded in Firefox.

ClientCode:

 
1:<DivID= "Content"Style="Width: 400px;Height: 200px;Border: 1px solid # ff0 "> </Div>
2:<ScriptType= "Text/JavaScript">
 
3:VaRComet = {
 
4:Connection:False,
 
5:Iframediv:False,
 
6: 
 
7:Initialize:Function(){
 
8:If(Navigator. appversion. indexof ("MSIE")! =-1 ){
9: 
 
10:// For IE browsers
 
11:Comet. Connection =NewActivexobject ("Htmlfile");
 
12:Comet. Connection. open ();
 
13:Comet. Connection. Write ("<HTML>");
 
14:Comet. Connection. Write ("<SCRIPT> document. Domain = '"+ Document. Domain +"'");
15:Comet. Connection. Write ("</Html>");
 
16:Comet. Connection. Close ();
 
17:Comet. iframediv = Comet. Connection. createelement ("Div");
 
18:Comet. Connection. appendchild (comet. iframediv );
 
19:Comet. Connection. parentwindow. Comet = comet;
 
20:Comet. iframediv. innerhtml ="<IFRAME id = 'comet _ IFRAME 'src = 'flush. aspx '> </iframe>";
 
21: 
22:}Else if(Navigator. appversion. indexof ("Khtml")! =-1 ){
 
23: 
 
24:// For khtml browsers
 
25:Comet. Connection = Document. createelement ('Iframe');
 
26:Comet. Connection. setattribute ('Id','Comet _ iframe');
27:Comet. Connection. setattribute ('Src','Flush. aspx');
 
28:With(Comet. Connection. Style ){
 
29:Position ="Absolute";
 
30:Left = Top ="-100px";
 
31:Height = width ="1px";
 
32:Visibility ="Hidden";
 
33:}
34:Document. Body. appendchild (comet. connection );
 
35: 
 
36:}Else{
 
37:// For other browser (Firefox ...)
 
38:Comet. Connection = Document. createelement ('Iframe');
 
39:Comet. Connection. setattribute ('Id','Comet _ iframe');
 
40:With(Comet. Connection. Style ){
41:Left = Top ="-200px";
 
42:Height = width ="1px";
 
43:Visibility ="Hidden";
 
44:Display ='None';
 
45:}
 
46:Comet. iframediv = Document. createelement ('Iframe');
47:Comet. iframediv. setattribute ('Src','Flush. aspx');
 
48:Comet. Connection. appendchild (comet. iframediv );
 
49:Document. Body. appendchild (comet. connection );
 
50: 
 
51:}
 
52:},
 
53: 
 
54:// This function will be called from backend. php
55:Getdata:Function(D ){
 
56:$ ('# Content'). Append (d );
 
57:},
 
58: 
 
59:OnUnload:Function(){
 
60:If(Comet. Connection ){
 
61:Comet. Connection =False;
62:// Release the IFRAME to prevent problems with IE when reloading the page
 
63:}
 
64:}
 
65:}
 
66: 
 
67:Comet. initialize ();
 
68: 
 
69:</Script>
 
70:

Server code:

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 \ "> window. Parent. Comet. getdata (\" {0} \ "); </SCRIPT>";
 
17:Response. Write (String. Format (data,"Start to send data: <br/>"));
 
18:Response. Flush ();
 
19: 
 
20:IntIndex = 0;
21:While(True)
 
22:{
 
23:System. threading.Thread. Sleep (2000 );
 
24:If(Index % 2 = 0)
 
25:{
 
26:Response. Write (String. Format (data,Datetime. Now. tostring ("Yyyy-mm-dd hh: mm: SS") +"Data sent by the server <br/>"));
 
27:}
28:Else
 
29:{
 
30:Response. Write (String. Format (data,Datetime. Now. tostring ("Yyyy-mm-dd hh: mm: SS") +"No data transmission <br/>"));
 
31:}
 
32:Response. Flush ();
 
33: 
 
34:Index ++;
 
35:}
 
36:}
  37:  

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.