Go的Flush()方法不起作用。

huangapple go评论69阅读模式
英文:

Go Flush() doesn't work

问题

请检查这个 gist 并告诉我,有什么问题?

为什么我看不到我的消息?

gist 链接:https://gist.github.com/cnaize/895f61b762a9f5ee074c

如果简单来说,我有两个函数:

func send(param martini.Params, r render.Render) {
    Ct.Msgs <- param["msg"]
    
    fmt.Printf("Sent: %v", param["msg"])
    
    r.JSON(http.StatusOK, Response{"status": "ok"})
}

func watch(rw http.ResponseWriter, r render.Render) {
    var msg string
    ok := true
    for ok {
        select {
        case msg, ok = <-Ct.Msgs:
            rw.Write([]byte(msg))
            fmt.Printf("Wrote: %v", msg)
            f, ok := rw.(http.Flusher)
            if ok {
                f.Flush()
                fmt.Println("Flushed")
            } else {
                r.JSON(http.StatusOK, Response{"status": "error", "descr": "CANT_FLUSH"})
                return
            }
        }
    }
    
    r.JSON(http.StatusOK, Response{"status": "ok", "descr": "finished"})
}

为什么它不起作用?

编辑:

我已经更新了我的 gist。

现在有:

if i, err := rw.Write([]byte(msg)); err != nil {
    r.JSON(http.StatusOK, Response{"status": "error", "descr": err.Error()})
    return
} else {
    fmt.Printf("i: %v", i)
}

在日志中我看到:

Sent: hello
i: 5
Wrote: hello
Flushed

但是我什么都看不到。

有什么想法吗?

英文:

Please, check this gist and tell me, what's wrong?
<br>
Why I don't see my messages?
<br>
The gist: https://gist.github.com/cnaize/895f61b762a9f5ee074c
<br>
<br>
If simple, I have two functions:

func send(param martini.Params, r render.Render) {
	Ct.Msgs &lt;- param[&quot;msg&quot;]
 
	fmt.Printf(&quot;Sent: %v&quot;, param[&quot;msg&quot;])
 
	r.JSON(http.StatusOK, Response{&quot;status&quot;: &quot;ok&quot;})
}

And watch function:

func watch(rw http.ResponseWriter, r render.Render) {
	var msg string
	ok := true
	for ok {
		select {
		case msg, ok = &lt;-Ct.Msgs:
			rw.Write([]byte(msg))
			fmt.Printf(&quot;Wrote: %v&quot;, msg)
			f, ok := rw.(http.Flusher)
			if ok {
				f.Flush()
				fmt.Println(&quot;Flushed&quot;)
			} else {
				r.JSON(http.StatusOK, Response{&quot;status&quot;: &quot;error&quot;, &quot;descr&quot;: &quot;CANT_FLUSH&quot;})
				return
			}
		}
	}
 
	r.JSON(http.StatusOK, Response{&quot;status&quot;: &quot;ok&quot;, &quot;descr&quot;: &quot;finished&quot;})
}

Why it doesn't work?

EDITED:
<br>
<br>
I've updated my gist.
Now where are:

if i, err := rw.Write([]byte(msg)); err != nil {
    r.JSON(http.StatusOK, Response{&quot;status&quot;: &quot;error&quot;, &quot;descr&quot;: err.Error()})
    return
} else {
    fmt.Printf(&quot;i: %v&quot;, i)
}

And I have in logs:

 Sent: hello
 i: 5
 Wrote: hello
 Flushed

But I see nothing.

Any ideas?

答案1

得分: 5

刷新正在工作。问题在于Chrome的纯文本渲染器在显示任何内容之前会等待完整的响应体。强制将内容类型设置为html以查看增量响应:

func watch(rw http.ResponseWriter, r render.Render) {
    rw.Header().Set("Content-Type", "text/html")
    // 之前的代码相同
}
英文:

The flush is working. The issue is that Chrome's plain text renderer waits for the complete response body before displaying anything. Force the content type to html to see the incremental response:

func watch(rw http.ResponseWriter, r render.Render) {
    rw.Header().Set(&quot;Content-Type&quot;, &quot;text/html&quot;)
    // same code as before
}

huangapple
  • 本文由 发表于 2014年10月13日 15:53:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/26335228.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定