利用Response.Flush和iframe實現”伺服器推”技術(續)

來源:互聯網
上載者:User
上一篇紹使用iframe實現”伺服器推”,這個方式有個缺點在IE、FireFox會出現進度列一直載入的狀態,上一篇也介紹在IE的解決方案,建議先看下上一篇的內容。網上的說http://www.zeitoun.net/articles/comet_and_php/start可以解決FireFox的問題,試了一下也只是IE下可以(也是使用htmlfile),FireFox還是會出現進度列一直載入狀態。

      用戶端代碼:

 1:  <div id="content" style=" width:400px; height:200px; border:1px solid #FF0"></div>
 2:     <script type="text/javascript">
 3:         var comet = {
 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 = new ActiveXObject("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:  

       服務端代碼:

 1:  protected void Page_Load(object sender, EventArgs e)
 2:     {
 3:         string startHTML = "<!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/1999/xhtml\" >" + Environment.NewLine
 5:             + "<head>" + Environment.NewLine
 6:             + "</head>" + Environment.NewLine
 7:             + "<body>" + Environment.NewLine;
 8:   
 9:         startHTML += new String(' ', 1024) + Environment.NewLine;
10:   
11:         Response.Write(startHTML);
12:         Response.Flush();
13:   
14:         string data = "<script type=\"text/javascript\">window.parent.comet.getData(\"{0}\");</script>";
17:         Response.Write(string.Format(data, "開始發送資料:<br/>"));
18:         Response.Flush();
19:   
20:         int index = 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") + " 服務端發送資料<br/>"));
27:             }
28:             else
29:             {
30:                 Response.Write(string.Format(data, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 無資料發送<br/>"));
31:             }
32:             Response.Flush();
33:   
34:             index++;
35:         }
36:     }
37:   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.