我正在處理asp.net項目中的服務器發送事件(Html5)。代碼非常簡單,因爲它從服務器接收數據並將其顯示在DIV中。 只有當我在FireFox上運行的代碼始終未調用Eventsource時,Firebug纔會顯示2個請求或12個請求,有時只顯示1個請求。 當我測試在鉻上的代碼,代碼工作正常,我有無限的請求,因爲我需要。 我的代碼是:服務器發送的事件EventSource不是在Firefox中不斷調用
<script type="text/javascript">
$(function()
{
if (window.EventSource == undefined)
{
$('#targetDiv').html('Your Browser Cant support Server Side Event');
return;
}
var source = new EventSource('GetData.aspx?userid=jalal');
source.onopen = function(event){$('#targetDiv').append("Connection opened... <br/>");};
source.onerror = function(event)
{
if (event.eventPhase == EventSource.CLOSED)
$('#targetDiv').append("Connection Closed!<br/>");
source.addEventListener("new-message", function (event) {});
};
source.onmessage = function(event){$('#targetDiv').append(event.data + '<br/>')};
});
</script>
<div id="targetDiv">
</div>
後面的頁面GetData.aspx.cs
代碼protected void Page_Load(object sender, EventArgs e)
{
DateTime StartDate = DateTime.Now;
Response.ContentType = "text/event-stream";
Response.Expires = -1;
Response.CacheControl = "no-cache";
Response.Write("data: " + Request.QueryString["userid"] + " " + DateTime.Now.ToString() + "\n\n ");
Response.Flush();
}
誰能給我的什麼是錯在上面的代碼的想法或爲什麼這只是發生在Firefox中。