Server Sent Events with ASP.Net is not communicating properly

3

Im trying to get SSE working. I have a simple web with two buttons. Each one send a POST request to the server that adds a message to a list.

When the eventsource is listening, the server checks the list once each second and sends all the available messages to the client, at the same time that it marks them as readed so they won't be sent again to that client.

It kind of works but does all sorts of weird stuff:

  • Sometimes the button POST requests are delayed for no apparent reason and then sent all at once.
  • Sometimes the EventSource restarts itself making a GET request to the server.
  • Sometimes the server generates an exception when calling Response.Flush() after spamming the buttons: "The remote host closed the connection. The error code is 0x800704CD"
  • After pressing the buttons a few times, when I try to reload the page, it stays "loading" forever.

After starting the EventSource in javascript it generates a GET request that stays open and after that, any POST request that the buttons send is never sent until the event source GET request ends. Once the EventSource connection ends, all POST requests from the buttons are sent.

I know a lot of things from this code can be improved but I simplified it a lot by leaving just the essential for it to "work".

Also note that:

  • NotificationSystem.GetNotifications() gets all messages available for the user in a thread safe way.
  • NotificationSystem.AddNotification() adds the messages.

So here is the server code:

      public void GetNotifs() {
        try {
            Response.ContentType = "text/event-stream";
            while(true) {
                List<string> Notifs = NotificationSystem.GetNotifications( GetUserId() );
                if(Notifs != null && Notifs.Count > 0) {
                    foreach(var Text in Notifs) {
                        Response.Write( string.Format( "data: {0}\n\n", Text ) );
                    }
                    Response.Flush();
                }
                Thread.Sleep( 1000 );
            }
        } catch(Exception ex) {
            Response.Close();
        }
    }
    public ActionResult AddButton1() {
        NotificationSystem.AddNotification( GetUserId(), "BTN1 (" + GetUserId() + ")" );
        return Json( "OK" );
    }
    public ActionResult AddButton2() {
        NotificationSystem.AddNotification( GetUserId(), "BTN2 (" + GetUserId() + ")" );
        return Json( "OK" );
    }

And the client JS code:

var urlMessages = "/Notifs/GetNotifs";
var urlButton1 = "/Notifs/AddButton1";
var urlButton2 = "/Notifs/AddButton2";

function PushBtn1() {
    $.post(urlButton1);
}
function PushBtn2() {
    $.post(urlButton2);
}

var myEventSource;
$(document).ready(function () {
    myEventSource = new EventSource(urlMessages);
    myEventSource.onmessage = function (e) {
        console.log(e.data);
        $('#EventLog').append("<li>Received: " + e.data + "<li>");
    }
});
javascript
c#
asp.net
server-sent-events
asked on Stack Overflow Feb 5, 2018 by Alriac

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0