【发布时间】:2012-10-31 19:39:20
【问题描述】:
当我尝试将 EventSource 连接到我的控制器时,它一直在说:
EventSource 的响应有一个 MIME 类型(“text/html”),它不是 “文本/事件流”。中止连接。
我做了以下课程来帮助处理和准备响应。在这个类中,我相信我将 responseType 设置为 text/event-stream。
public class ServerSentEventResult : ActionResult
{
public delegate string GetContent();
public GetContent Content { get; set; }
public int Version { get; set; }
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
if (this.Content != null)
{
HttpResponseBase response = context.HttpContext.Response;
response.ContentType = "text/event-stream"; response.BufferOutput = false; response.Charset = null;
string[] newStrings = context.HttpContext.Request.Headers.GetValues("Last-Event-ID");
if (newStrings == null || newStrings[0] != this.Version.ToString())
{
try
{
response.Write("retry:250\n");
response.Write(string.Format("id:{0}\n", this.Version));
response.Write(string.Format("data:{0}\n\n", this.Content()));
response.End();
}
catch (HttpException e) { }
}
else
{
response.Write(String.Empty);
}
}
}
}
有人可以帮我解决这个问题吗?万分感谢!
【问题讨论】:
标签: c# javascript asp.net-mvc mime-types