update stream example

Former-commit-id: a62ee01e0811f2595e292b3d50b844d999ccb4d3
This commit is contained in:
Gerasimos (Makis) Maropoulos 2020-07-01 14:43:24 +03:00
parent 134e2531bf
commit 2d9485326b
3 changed files with 25 additions and 14 deletions

View File

@ -58,7 +58,7 @@ $ go run main.go
</details>
[![run in the browser](https://img.shields.io/badge/Run-in%20the%20Browser-348798.svg?style=for-the-badge&logo=repl.it)](https://repl.it/@kataras/Iris-Hello-World)
[![run in the browser](https://img.shields.io/badge/Run-in%20the%20Browser-348798.svg?style=for-the-badge&logo=repl.it)](https://bit.ly/2YJeSZe)
Iris contains extensive and thorough **[wiki](https://github.com/kataras/iris/wiki)** making it easy to get started with the framework.

View File

@ -18,7 +18,8 @@ func main() {
ctx.Header("Transfer-Encoding", "chunked")
i := 0
ints := []int{1, 2, 3, 5, 7, 9, 11, 13, 15, 17, 23, 29}
// Send the response in chunks and wait for half a second between each chunk.
// Send the response in chunks and wait for half a second between each chunk,
// until connection close.
err := ctx.StreamWriter(func(w io.Writer) error {
ctx.Writef("Message number %d<br>", ints[i])
time.Sleep(500 * time.Millisecond) // simulate delay.
@ -40,20 +41,30 @@ func main() {
Number int `json:"number"`
}
app.Get("/alternative", func(ctx iris.Context) {
app.Get("/json", func(ctx iris.Context) {
ctx.Header("Transfer-Encoding", "chunked")
i := 0
ints := []int{1, 2, 3, 5, 7, 9, 11, 13, 15, 17, 23, 29}
// Send the response in chunks and wait for half a second between each chunk.
// Send the response in chunks and wait for half a second between each chunk,
// until connection close.
notifyClose := ctx.Request().Context().Done()
for {
ctx.JSON(messageNumber{Number: ints[i]})
ctx.WriteString("\n")
time.Sleep(500 * time.Millisecond) // simulate delay.
if i == len(ints)-1 {
break
select {
case <-notifyClose:
// err := ctx.Request().Context().Err()
ctx.Application().Logger().Infof("Connection closed, loop end.")
return
default:
ctx.JSON(messageNumber{Number: ints[i]})
ctx.WriteString("\n")
time.Sleep(500 * time.Millisecond) // simulate delay.
if i == len(ints)-1 {
ctx.Application().Logger().Infof("Loop end.")
return
}
i++
ctx.ResponseWriter().Flush()
}
i++
ctx.ResponseWriter().Flush()
}
})

6
go.mod
View File

@ -29,15 +29,15 @@ require (
github.com/ryanuber/columnize v2.1.0+incompatible
github.com/schollz/closestmatch v2.1.0+incompatible
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/square/go-jose/v3 v3.0.0-20200622023058-052237293361
github.com/square/go-jose/v3 v3.0.0-20200630053402-0a67ce9b0693
github.com/vmihailenco/msgpack/v5 v5.0.0-beta.1
go.etcd.io/bbolt v1.3.5
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
golang.org/x/net v0.0.0-20200625001655-4c5254603344
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae
golang.org/x/text v0.3.3
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1
google.golang.org/protobuf v1.24.0
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e
google.golang.org/protobuf v1.25.0
gopkg.in/ini.v1 v1.57.0
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
)