Browse Source

Issue #78: Added an example for RequestCtx.SetBodyStreamWriter

Aliaksandr Valialkin 5 years ago
parent
commit
05949704db
1 changed files with 32 additions and 0 deletions
  1. 32 0
      requestctx_setbodystreamwriter_example_test.go

+ 32 - 0
requestctx_setbodystreamwriter_example_test.go

@@ -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)
+		}
+	})
+}