我应该在Golang http.server中配置什么来处理SSE

http.server can be configure with these parameters:

ReadTimeout, WriteTimeout,IdleTimeout and others

I was just playing with parameters to see in which cases would be better to configure with SSE, so following the same structured from here I just add some prints to see when the clients conects and when disconects, like:

 ....
    notify := rw.(http.CloseNotifier).CloseNotify()
     fmt.Fprintf(w, "data: .

") `//I add this because [onopen][3] won´t` fire if nothing is sent when start conecting
    fmt.Println(" conecting")
        for {
            select {
            case <-notify:
    fmt.Println("closing")
                return
            default:

                // Write to the ResponseWriter
                // Server Sent Events compatible
                fmt.Fprintf(rw, "data: %s

", <-messageChan)

                // Flush the data immediatly instead of buffering it for later.
                flusher.Flush()
            }
        } 

So the only value that if I changed, it prints "closing" is WriteTimeout but reading this article it is confuse to me this part:

Finally, Go 1.8 introduces IdleTimeout which limits server-side the amount of time a Keep-Alive connection will be kept idle before being reused

But as I said even if I change IdleTimeout the client won't close, is it because the first time the client conects I write to it, or is it a bug? Which of three should I change then? Go version: 1.11.1

douliang2167
douliang2167 即使发送正确的标题也会发生这种情况
接近 2 年之前 回复
doutou3725
doutou3725 我正在发送正确的标题
接近 2 年之前 回复
dta25920
dta25920 实际上确实需要配置,就像我说的,如果您更改“WriteTimeout”,这是连接关闭且服务器不会发送任何数据的时间,您可以从链接尝试相同的示例,并设置“WriteTimeout”到例如10秒钟,您将看到服务器不会发送任何数据,因为10秒钟过去了,但是我不知道为什么在这种情况下,如果“IdleTimeout”应该处理这种情况而不是WriteTimeout
接近 2 年之前 回复
douyasihefu6214
douyasihefu6214 解决方案中可能缺少的一件事:您必须在发送第一个数据帧之前设置某些响应标头,例如ContentType=text/event-stream和Connection=keep-alive。有关详细信息,请参见链接的答案。
接近 2 年之前 回复
douxian5076
douxian5076 支持SSE不需要特殊处理,有关详细信息和示例,请参阅为什么Golanghttp.ResponseWriter执行被延迟?您的客户端很可能关闭连接,而不是服务器。尽管如果中间连接处于空闲状态,中间网关和代理可能会关闭它,因此,即使没有数据要发送,也要定期发送“ping”消息,这是一个好习惯。
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐