We can easily implement HTTP streaming data return.
Framework version < v2.4
If the current framework version is less than v2.4
final version (not beta
version), use the following method to return (native standard library writing).
package main
import (
"fmt"
"net/http"
"time"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
)
func main() {
s := g.Server()
s.BindHandler("/", func(r *ghttp.Request) {
rw := r.Response.RawWriter()
flusher, ok := rw.(http.Flusher)
if !ok {
http.Error(rw, "Streaming unsupported!", http.StatusInternalServerError)
return
}
r.Response.Header().Set("Content-Type", "text/event-stream")
r.Response.Header().Set("Cache-Control", "no-cache")
r.Response.Header().Set("Connection", "keep-alive")
for i := 0; i < 100; i++ {
_, err := fmt.Fprintf(rw, "data: %d\n", i)
if err != nil {
panic(err)
}
flusher.Flush()
time.Sleep(time.Millisecond * 200)
}
})
s.SetPort(8999)
s.Run()
}
Framework version >= v2.4
Because the above operation is somewhat cumbersome, operational improvements were made in this version and above. If the current framework version is v2.4
final version (not beta
version) or later, you can quickly implement streaming data return using the following method.
package main
import (
"time"
"github.com/gogf/gf/v2/frame/g"
"github.com/gogf/gf/v2/net/ghttp"
)
func main() {
s := g.Server()
s.BindHandler("/", func(r *ghttp.Request) {
r.Response.Header().Set("Content-Type", "text/event-stream")
r.Response.Header().Set("Cache-Control", "no-cache")
r.Response.Header().Set("Connection", "keep-alive")
for i := 0; i < 100; i++ {
r.Response.Writefln("data: %d", i)
r.Response.Flush()
time.Sleep(time.Millisecond * 200)
}
})
s.SetPort(8999)
s.Run()
}
Example Result
After execution, visiting http://127.0.0.1:8999/ shows data continuously returned to the caller in a streaming manner.
Precautions
- If used in a controller, the
Request
object can be acquired through theg.RequestFromCtx(ctx)
method. - If there is a middleware for pre-unified input and output processing, please place this method outside the middleware scope or use the
r.ExitAll()
method to exit middleware control.