这是我的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。任何人都可以帮我解决这个问题吗?
答案 0 :(得分:0)
尝试在冒号后删除空格。似乎Firefox解析器对空格和换行非常敏感。尝试更改您的代码:
Response.Write(string.Format("retry:5000\nid:{0}\ndata:{0}\n\n", MessageCount));
在某些浏览器中定义字符集也有帮助:
Response.ContentType = "text/event-stream;charset=UTF-8";
希望这有帮助。