簡體   English   中英

更改Server-Sent事件的源(url)

[英]Change source (url) of Server-Sent event

如何更改EventSource聲明中的源集?

我試過這樣的事情:

var source = new EventSource("/blahblah.php?path=" + (window.location.pathname));
// Few lines later...
source.url = "/blahblah.php?path=" + url;

但是, source.url保持不變......

這甚至可能嗎? 或者也許有其他方法可以做到這一點?

不,這是不可能的。 傳遞給EventSource()新URL會創建一個新的EventSource對象。

guest271314向我提出了很多解決方案,但對我來說,與sse保持相比會更好,更快,更簡單。

我通過關閉和刪除(所有必要的)源var來更改源。

這就是我做的方式:

source.url = "/blahblah2.php";

source.close();
delete source;

//Redefine source

重要提示:如果使用var定義source,則無效! 重新定義也應該沒有它!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM