I want to know how to close a server-sent events via JavaScript. Below is my dummy code:
var ser=new EventSource("path");
ser.onmessage=function(ev){
if(!ev)
//want to close HERE!!
else
console.log(ev);
}
Here is the way that I found worked best for me
var eventSource = new EventSource("path");
eventSource.onerror = eventSourceErrorFunction;
var eventSourceErrorFunction = function(event)
{
if (event.eventPhase == EventSource.CLOSED) {
that.eventSource.close();
console.log("Event Source Closed");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With