大猩猩mux将流数据发送到浏览器时,并没有逐个发送项目。

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

Gorilla mux streaming data to browser is not sending item one by one

问题

我正在尝试将数据缓冲到浏览器中。我的意图是每隔1秒发送一个项目。然而,当我在浏览器中测试时,它只是等待所有延迟完成,然后一次性显示结果。

以下是在gorilla框架中使浏览器逐个显示数据的示例:

  1. func StreamHandler(w http.ResponseWriter, r *http.Request) {
  2. log.Println("string handler invoked")
  3. flusher, _ := w.(http.Flusher)
  4. for i := 0; i < 15000; i++ {
  5. fmt.Println(i)
  6. fmt.Fprintf(w, "Gorilla! %v \n", i)
  7. flusher.Flush()
  8. time.Sleep(1 * time.Second)
  9. }
  10. fmt.Println("done")
  11. }

你可以尝试将这段代码添加到你的gorilla框架中,看看是否能够按照预期逐个显示数据。

英文:

I am trying to buffer data to browser. My intention is to send each item one by one with a delay of 1 second. However when I test in browser its just wait for all the delays to complete and show the result at once.

  1. func StreamHandler(w http.ResponseWriter, r *http.Request) {
  2. log.Println(&quot;string handler invoked&quot;)
  3. flusher, _ := w.(http.Flusher)
  4. for i := 0; i &lt; 15000; i++ {
  5. // w.Write([]byte(&quot;Gorilla! \n&quot;))
  6. fmt.Println(i)
  7. fmt.Fprintf(w, &quot;Gorilla! %v \n&quot;, i)
  8. flusher.Flush()
  9. time.Sleep(1 * time.Second)
  10. // time.Sleep(1 * time.Second)
  11. }
  12. fmt.Println(&quot;done&quot;)
  13. }

Similar thing is super each to do with echo web framework. following example in echo framework makes browser to show data one by one

  1. e.GET(&quot;/&quot;, func(c echo.Context) error {
  2. c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
  3. c.Response().WriteHeader(http.StatusOK)
  4. enc := json.NewEncoder(c.Response())
  5. for _, l := range locations {
  6. if err := enc.Encode(l); err != nil {
  7. return err
  8. }
  9. c.Response().Flush()
  10. time.Sleep(1 * time.Second)
  11. }
  12. return nil
  13. })

Please help me out to make it work in gorilla framework.

答案1

得分: 1

以下是代码的翻译:

所以以下代码是有效的:

  1. log.Println("string handler invoked")
  2. flusher, ok := w.(http.Flusher)
  3. if !ok {
  4. log.Println("responseWriter is not really a flusher")
  5. return
  6. }
  7. // 这个头部没有效果
  8. w.Header().Set("Connection", "Keep-Alive")
  9. // 这两个头部是为了逐步获取HTTP块而需要的
  10. w.Header().Set("Transfer-Encoding", "chunked")
  11. w.Header().Set("X-Content-Type-Options", "nosniff")
  12. for i := 0; i < 20; i++ {
  13. // w.Write([]byte("Gorilla! \n"))
  14. fmt.Println(i)
  15. fmt.Fprintf(w, "Gorilla! %v \n", i)
  16. flusher.Flush()
  17. time.Sleep(1 * time.Second)
  18. // time.Sleep(1 * time.Second)
  19. }
  20. fmt.Println("done")
英文:

So following code works

  1. log.Println(&quot;string handler invoked&quot;)
  2. flusher, ok := w.(http.Flusher)
  3. if !ok {
  4. log.Println(&quot;responseWriter is not really a flusher&quot;)
  5. return
  6. }
  7. //this header had no effect
  8. w.Header().Set(&quot;Connection&quot;, &quot;Keep-Alive&quot;)
  9. //these two headers are needed to get the http chunk incremently
  10. w.Header().Set(&quot;Transfer-Encoding&quot;, &quot;chunked&quot;)
  11. w.Header().Set(&quot;X-Content-Type-Options&quot;, &quot;nosniff&quot;)
  12. for i := 0; i &lt; 20; i++ {
  13. // w.Write([]byte(&quot;Gorilla! \n&quot;))
  14. fmt.Println(i)
  15. fmt.Fprintf(w, &quot;Gorilla! %v \n&quot;, i)
  16. flusher.Flush()
  17. time.Sleep(1 * time.Second)
  18. // time.Sleep(1 * time.Second)
  19. }
  20. fmt.Println(&quot;done&quot;)

huangapple
  • 本文由 发表于 2022年11月8日 16:09:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/74357489.html
匿名

发表评论

匿名网友

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

确定