Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,

I have been trying to implement an event driven push to a client browser. I am using ReactiveX to produce the async task from the events but I cannot get my HttpHandlers to output their response.

I have tried with a simple HttpHandler:
C#
public class Handler2 : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/event-stream";
        HttpResponse response = context.Response;

        DateTime startdate = DateTime.Now;

        while (startdate.AddMinutes(10) > DateTime.Now)
        {
            JavaScriptSerializer js = new JavaScriptSerializer();

            string responseText = DateTime.Now.TimeOfDay.ToString();

            response.Write(string.Format("data: {0}",js.Serialize(responseText)));
            response.Flush();
            System.Threading.Thread.Sleep(1000);
        }
        response.Close();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}


with the following client side code:
JavaScript
function initialize() {

    if (window.EventSource == undefined) {
        document.getElementById('targetDiv').innerHTML = "Your browser doesn't support Server Side Events.";
        return;
    }

    var source = new EventSource('Handler2.ashx');

    source.onopen = function (event) {
        document.getElementById('targetDiv').innerHTML += 'Connection Opened.<br>';
    };

    source.onerror = function (event) {
        if (event.eventPhase == EventSource.CLOSED) {
            document.getElementById('targetDiv').innerHTML += 'Connection Closed.<br>';
        }
    };

    source.onmessage = function (event) {
        document.getElementById('targetDiv').innerHTML += event.data + '<br>';
    };
}


I have more a more complex HttpTaskAsyncHandler ready to hook up, but I can't even get this working >_<<br mode="hold" />
I get the Connection Opened message, Handler2.ashx appears to remain connected (Looking at Chrome dev tools / Network).

I am, on the other hand, getting some data from a SignalR connection?
ws://localhost:50022/ed4b66c7eb394a8789b5f6a631f4ff09/arterySignalR/connect

Have I set it up wrong?


As far as I've seen on other examples, this code should be working as-is. Please could anyone help me. I just want a simple SSE control that I can trigger from server side events.

Thanks in advance
Posted
Comments
jeremygreen 18-Oct-15 9:26am    
did you ever sort this as I have virtually the same sample and it does not work.
Andy Lanng 19-Oct-15 5:46am    
Yes I did. I abandoned ReactiveX and used SignalR instead. It's much better anyway :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900