Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Server Sent Event and asp classic

I made a ASP script with all headers necessary for Server Sent Events. The client side script is working. I tested it with a PHP script and it works fine. The ASP script works and it returns the expected message if you call it from the URL line, but when the javascript calls it, the element inspector returns the data as "undefined". When I set the breakpoint to the "open" event listener, the "e.data" is "undefined".

For some reason Javascript is not accepting the UTF-8 text sent by the ASP script.

Help me please!!!

The asp code:

strResponse = strResponse &  "data: " & now() 
response.Buffer = true
response.CharSet = "utf-8"
response.ContentType="text/event-stream"    
response.AddHeader  "content-Type", "text/event-stream;charset=UTF-8"
response.AddHeader  "Cache-Control", "no-cache"      
response.AddHeader  "Content-Length", len(strResponse)
response.AddHeader  "Connection", "Keep-Alive"
response.Write strResponse

The Javascript:

if (!!window.EventSource) {
    var source = new EventSource('serversend.asp');
    source.addEventListener('message', function (e) {
        console.log(e.data);
    }, false);

    source.addEventListener('open', function (e) {
        console.log("open " + e.data);
    }, false);

    source.addEventListener('error', function (e) {
        if (e.readyState == EventSource.CLOSED) {
            console.log(e.data);
        }
    }, false);



} else {
    // something else :(
}

Thank you!

like image 412
AndreSites Avatar asked Oct 30 '25 19:10

AndreSites


1 Answers

Try to use Response.Flush after Response.Write

Edit* Put two enters after the strResponse string: response.Write strResponse & vbCrLf & vbCrLf

like image 65
Deni Marinho Avatar answered Nov 01 '25 11:11

Deni Marinho



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!