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

來源:互聯網
上載者:User
通過在HTML頁面裡陷入一個隱藏的iframe,然後將這個iframe的src屬性設為對一個長串連的請求(利用chunked傳輸response),伺服器端就能源源不斷地往客戶推送資料。

      基於流方式的”伺服器推”模型:

       服務端在接到用戶端的請求時,通過Response的Flush()方法發送資料,可以使用定時器的方式發送資料,沒有資料也發送”無資料”,讓用戶端保持長串連,直至用戶端中斷連線,請求結束。每次資料傳送不會關閉串連,串連只會在通訊出現錯誤時,或是串連重建時關閉(一些防火牆常被設定為丟棄過長的串連, 伺服器端可以設定一個逾時時間, 逾時後通知用戶端重建立立串連,並關閉原來的串連)。

      實現代碼:

      頁面Default.aspx,用來展示資料:

1:     資料列表:o<br />
2:     <div id="con" style=" width:400; height:200px; border:1px solid #FF0">
3:     </div>
4:     <iframe id="flush" src="Flush.aspx" style=" display:none" />
5:  

     ifame的src對應的Flash.aspx後台代碼,類比後台發送資料:

 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\">parent.$('#con').append(\"{0}\");</script>";
15:          Response.Write(string.Format(data, "開始發送資料:<br/>"));
16:          Response.Flush();
17:   
18:          int index = 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") + " 服務端發送資料<br/>"));
25:              }
26:              else
27:              {
28:                  Response.Write(string.Format(data, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " 無資料發送<br/>"));
29:              }
30:              Response.Flush();
31:   
32:              index++;
33:          }
34:      }
35:   

      運行Default.aspx的結果:

      使用 iframe 請求一個長串連有一個很明顯的不足之處:IE、Morzilla Firefox 下端的進度列都會顯示載入沒有完成,而且 IE 上方的表徵圖會不停的轉動,表示載入進行中;重新整理當前頁面反應也是會很慢。

      解決IE的進度列顯示載入沒有完成,可以使用一個稱為“htmlfile”的 ActiveX,是Google 的天才們使用的方法,該控制項也被用到gmail+gtalk 產品中。

      修改Default.aspx的頁面代碼:

 1:  數y據Y列D表í:o<br />
 2:     <div id="con" style=" width:400; height:200px; border:1px solid #FF0">
 3:     </div>
 4:      <script type="text/javascript">
 5:          function getData(d)
 6:          {
 7:              $("#con").append(d);
 8:          }
 9:   
10:          function rpc_iframe() {
11:              var transferDoc = new ActiveXObject("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);  //不加這句會使串連斷開
18:          }
19:   
20:          rpc_iframe();
21:     </script>
22:  

     修改Flush.aspx.cs代碼:

1:  //string data = "<script type=\"text/javascript\">parent.$('#con').append(\"{0}\");</script>";
2:          string data = "<script type=\"text/javascript\">parent.getData(\"{0}\");</script>";
3:   

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.