上交所未注册

我正在尝试将事件从服务器发送到客户端,并在客户端进行注册。 </ p>

我的转到代码:</ p>

  w.Header()。Set(“ Content-Type”,“ text / event-stream”  )
w.Header()。Set(“ Cache-Control”,“ no-cache”)
w.Header()。Set(“ Connection”,“ keep-alive”)
w.Header()。Set( “ Access-Control-Allow-Origin”,“ *”)

fmt.Fprintf(w,“ id:%v
event:msg
message:%v

”,id,“ hello”)\ nfmt.Printf(“ id:%v
event:msg
message:%v

”,id,“ hello”)

id ++
</ code> </ pre>

客户端JS:</ p>

 函数dump_event(e){
console.log(“ received event”)
$(“#output”)。append(“ id :“ + e.lastEventId +”
event:“ + e.type +”
message:“ + e.data +”

“);
}

$(function(){
$(“#listen”)。on('click',函数(e){
e.preventDefault()
if(source!= null){
source.close();
}
源 = new EventSource($(“#backend”)。val()+ $(“#channel”)。val());
source.addEventListener('msg',dump_event);
source.addEventListener('timeout ',dump_event);
});
</ code> </ pre>

尽管提琴手会 我实际上是如何接收数据的,它不是控制台记录的。 我真的很感谢您的帮助,我在做错什么吗?</ p>
</ div>

展开原文

原文

I am trying to send an event from the server to the client and register it on their end.

my go code:

w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Access-Control-Allow-Origin", "*")

fmt.Fprintf(w, "id: %v
event: msg
message: %v

", id, "hello")
fmt.Printf("id: %v
event: msg
message: %v

", id, "hello")

id++

Client side JS:

function dump_event(e) {
    console.log("received event")
    $("#output").append("id: " + e.lastEventId + "
event: " +e.type + "
message: " + e.data + "

");
}

$(function() {
    $("#listen").on('click', function (e) {
        e.preventDefault()
        if (source != null) {
            source.close();
        }
        source = new EventSource($("#backend").val() + $("#channel").val());
        source.addEventListener('msg', dump_event);
        source.addEventListener('timeout', dump_event);
    });

While fiddler does show that I actually received data, it is not console logged. I would really appreciate some help, am I doing something wrong?

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐