长期饭票

大家好,请喊我序员!
QQ:15838986
  首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
上一篇绍使用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: