replace zxq.co/ripple/hanayo
This commit is contained in:
32
vendor/github.com/valyala/fasthttp/requestctx_setbodystreamwriter_example_test.go
generated
vendored
Normal file
32
vendor/github.com/valyala/fasthttp/requestctx_setbodystreamwriter_example_test.go
generated
vendored
Normal file
@@ -0,0 +1,32 @@
|
||||
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)
|
||||
}
|
||||
})
|
||||
}
|
Reference in New Issue
Block a user