这是我用于服务器发送事件的 ASP.NET。
Response.ContentType = "text/event-stream";
Response.CacheControl = "no-cache";
//repeat loop
while (true) {
int LastEventId = Request.Headers.Get("Last-Event-ID") != null ?
Int32.Parse(Request.Headers.Get("Last-Event-ID")) : -1;
int MessageCount = UserManager.GetMessageCount(UserManager.GetUserDetails(Page.User.Identity.Name).UserId);
if (LastEventId != MessageCount)
{
Response.Write(string.Format("retry: 5000\nid: {0}\ndata: {0}\n\n", MessageCount));
Response.Flush();
}
else
{
Response.Write(string.Format("retry: 5000\nid: {0}\ndata: \n\n", MessageCount));
Response.Flush();
}
if (Response.IsClientConnected == false)
{
break;
}
Response.Close();
System.Threading.Thread.Sleep(5000);
}
Firefox 浏览器在两次后没有重新连接。相同的代码在 Google Chrome 上运行。任何人都可以帮我解决这个问题吗?