SSE vs flush js?

One of the signalR backup signals is events sent by the SSE server. which all he does is send a message through this structure:

Message\n\n

therefore, on the server side:

Response.ContentType = "text/event-stream";

while (startDate.AddMinutes(1) > DateTime.Now)
{
    Response.Write(string.Format("data: {0}\n\n", DateTime.Now.ToString()));
    Response.Flush();

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

big.

but not so much.

I could easily use a JS command with data (e.g. jsonp does aka: padding with a callback analogy), for example: _ cb(myMessage)

And on the client side - treat it like that. (even with the more acclaimed mime type like application / javascript)

I probably missed something.

  • What is the advantage of SSE?
+4
source share
1 answer

Server-sent events (SSE) JSONP/ , SSE , - .

JSONP, <script src="..., JSONP, JavaScript , . , , JSONP, .

, , , JSONP (.. script) . JSONP .

, , iframe script JavaScript. , JavaScript , , SSE.

, iframe - , SignalR . , , SignalR SSE , :

  • iframe *, HTML- JavaScript.

  • script . (, <script>c({"message": "myMessage"})</script>). SSE data: {"message": "myMessage"}\n\n, .

  • , , , , , iframe, , DOM. , , iframe . EventSource, SSE, .

* SignalR forever-frame prelude:

<!DOCTYPE html><html><head><title>SignalR Forever Frame Transport Stream</title>
<script>
    var $ = window.parent.jQuery,
        ff = $ ? $.signalR.transports.foreverFrame : null,
        c =  ff ? ff.getConnection('1') : null,
        r = ff ? ff.receive : function() {};
        ff ? ff.started(c) : '';</script></head><body>
+5

Source: https://habr.com/ru/post/1538706/


All Articles