服务器发送的事件处理程序无效

时间:2015-11-02 09:23:21

标签: javascript c# asp.net handler server-sent-events

我的网站有问题

我需要推送通知,我使用服务器发送的事件javascript和Handler.ashx

我的NotificationHandler.ashx

public class NotificationHandler : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
    public void EndProcessRequest(IAsyncResult result)
    {
    }

    public void ProcessRequest(HttpContext context)
    {

        context.Response.ContentType = "text/event-stream";
        context.Response.CacheControl = "no-cache";

        var _json = new JsonStringSerializer();
        var _chat = new ChatManager();

        while (true)
        {
            var GetStorage = _chat.GetStorage();
            var GetJson = _json.SerializeToString<ChatUserStorage>(GetStorage);

            context.Response.Write(string.Format("data: {0}\n\n", GetJson));
            context.Response.Flush();

            if (context.Response.IsClientConnected == false)
            {
                break;
            }

            System.Threading.Thread.Sleep(3000);
        }

    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

JavaScript:

function ServerSentNotification() {

sourceEventSource = new EventSource('/services/NotificationHandler.ashx', { retry: 1000 });

sourceEventSource.addEventListener("open", function (event) {
}, false);

sourceEventSource.addEventListener("error", function (event) {
    if (event.eventPhase == EventSource.CLOSED) {
    }
}, false);

sourceEventSource.addEventListener("message", function (event) {

    UpdateProgress(event.data);
}, false);

}

所有请求都停留在我的页面上,另一个ajax轮询没有工作请求等待。

我使用Web应用程序框架4.0

0 个答案:

没有答案