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

How can I change the source set in declaration of EventSource?I have tried something like this:var sour

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

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745672803a4639509.html

相关推荐

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

    How can I change the source set in declaration of EventSource?I have tried something like this:var sour

    6小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信