admin管理员组

文章数量:1435859

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?

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?

Share Improve this question asked Feb 24, 2017 at 18:50 user5147563user5147563
Add a ment  | 

2 Answers 2

Reset to default 5

No, it is not possible. A new URL passed to EventSource() creates a new EventSource object.

I Do it like this.

var source;

function ChangeEventSource(Url) {

    if (source != undefined) {source.close(); }
    
    if (typeof (EventSource) !== "undefined") {
    
        source = new EventSource(Url);
        source.onmessage = function (event) { 
                //Do something here
        };
        
    } else {
        console.log("Sorry, your browser does not support server-sent events...");
    }
}

本文标签: javascriptChange source (url) of ServerSent eventStack Overflow