$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>javascript - Change source (url) of Server-Sent event - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Change source (url) of Server-Sent event - Stack Overflow

matteradmin11PV0评论

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...");
    }
}
Post a comment

comment list (0)

  1. No comments so far