How to stop Server-Sent Events

前端 未结 2 987
长情又很酷
长情又很酷 2020-12-19 16:29

Hello I have a javascript code that listens to a PHP code via Server-Sent Events, it is working well the response is sent from the server trough a loop and when the loop end

2条回答
  •  轮回少年
    2020-12-19 17:09

    Hi I've added extra to (hopefully fix this) You have to pass from the server a message to close the connection. Otherwise when the execution finishes the browser will start with a new connection

    air code

    JS :

    var sse=new EventSource("data.php");
    sse.onmessage=function(event){
        if ('END-OF-STREAM' == event.data) {
            sse.close(); // stop retry
        }
        document.getElementById("map").innerHTML+=event.data;
    };
    

    PHP:

提交回复
热议问题