1 Star 0 Fork 0

ap6108/fasthttp

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
requestctx_setbodystreamwriter_example_test.go 742 Bytes
一键复制 编辑 原始数据 按行查看 历史
package fasthttp_test
import (
"bufio"
"fmt"
"log"
"time"
"github.com/valyala/fasthttp"
)
func ExampleRequestCtx_SetBodyStreamWriter() {
// Start fasthttp server for streaming responses.
if err := fasthttp.ListenAndServe(":8080", responseStreamHandler); err != nil {
log.Fatalf("unexpected error in server: %s", err)
}
}
func responseStreamHandler(ctx *fasthttp.RequestCtx) {
// Send the response in chunks and wait for a second between each chunk.
ctx.SetBodyStreamWriter(func(w *bufio.Writer) {
for i := 0; i < 10; i++ {
fmt.Fprintf(w, "this is a message number %d", i)
// Do not forget flushing streamed data to the client.
if err := w.Flush(); err != nil {
return
}
time.Sleep(time.Second)
}
})
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/ap6108/fasthttp.git
git@gitee.com:ap6108/fasthttp.git
ap6108
fasthttp
fasthttp
master

搜索帮助

D67c1975 1850385 1daf7b77 1850385