forked from gin-gonic/examples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
send_chunked_data.go
46 lines (41 loc) · 773 Bytes
/
send_chunked_data.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"fmt"
"github.com/gin-gonic/gin"
"log"
"net/http"
"time"
)
func main(){
log.SetFlags(log.LstdFlags | log.Lshortfile)
r := gin.Default()
r.GET("/test_stream", func(c *gin.Context){
w := c.Writer
header := w.Header()
header.Set("Transfer-Encoding", "chunked")
header.Set("Content-Type", "text/html")
w.WriteHeader(http.StatusOK)
w.Write([]byte(`
<html>
<body>
`))
w.(http.Flusher).Flush()
for i:=0 ;i<10; i++{
w.Write([]byte(fmt.Sprintf(`
<h1>%d</h1>
`,i)))
w.(http.Flusher).Flush()
time.Sleep(time.Duration(1) * time.Second)
}
w.Write([]byte(`
</body>
</html>
`))
w.(http.Flusher).Flush()
})
r.Run("127.0.0.1:8080")
}
/*
browser test url:
http://127.0.0.1:8080/test_stream
*/