问题
How can I change the source set in declaration of EventSource
?
I have tried something like this:
var source = new EventSource("/blahblah.php?path=" + (window.location.pathname));
// Few lines later...
source.url = "/blahblah.php?path=" + url;
But, source.url
stays the same...
Is this even possible? Or maybe there are alternative ways to do that?
回答1:
No, it is not possible. A new URL passed to EventSource()
creates a new EventSource
object.
回答2:
guest271314 suggested me much solutions to this, but for me it would be much better, faster and less complicated to stay with sse.
I changed the source by closing and removing (all are necessary) the source var.
This is how I did it:
source.url = "/blahblah2.php";
source.close();
delete source;
//Redefine source
IMPORTANT: This won't work if you defined source with var
! Redefinition should also go without it!
来源:https://stackoverflow.com/questions/42446195/change-source-url-of-server-sent-event