How to stop Server-Sent Events

前端 未结 2 991
长情又很酷
长情又很酷 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:17

    You could use a boolean variable isOpenOnce = false in your javascript and then in your sse.onopen set isOpen to true the first time. In the beginning check the flag and close connection if true. This way no need of server side changes.

    var isOpenOnce = false;
    sse.onopen = function() {
     if(isOpenOnce) {
      sse.close();
     }else {
      console.log("Connection to server opened.");
      isOpenOnce = true;
     }
    }
    

提交回复
热议问题