简体   繁体   中英

Server Sent Events Listeners not being hit?

I just started using SSE, and I have it somewhat implemented. I'm able to make a http request to the server, but every response I get is going back to the onmessage function. I can't seem to figure out why my custom listeners aren't being hit. I have a listener for "ping" and a listener for "pong". In my python script I have two events set... One for "ping", and the other for "pong". What am I missing?

    print "Content-Type: text/event-stream"             
    print "Cache-Control: no-control"                   
    while True:                                             
            print "event: ping\n"                       
            print "id: " + str(randint(0,9))            
            print "data: Hello World!\n"                
            print "\n\n"                                                            
            sys.stdout.flush()

source.onopen = function() {
        alert("On open");
};

source.addEventListener("ping", function(e)
{
        alert("On ping");
}, false);

source.addEventListener("pong", function(e)
{
        alert("On pong");
}, false);

source.onmessage = function(event)
{
        alert("On message");
};

I checked the response header, and it contains event: ping. Not sure why the event listener isn't picking it up?

I ended up figuring it out, thank you anyway. Once I removed the \\n from print "event: ping\\n" the issue was solved.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM