Change source (url) of Server-Sent event

こ雲淡風輕ζ 提交于 2019-11-29 22:35:20

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!