mirror of
https://github.com/kataras/iris.git
synced 2025-02-02 15:30:36 +01:00
update stream example
Former-commit-id: a62ee01e0811f2595e292b3d50b844d999ccb4d3
This commit is contained in:
parent
134e2531bf
commit
2d9485326b
|
@ -58,7 +58,7 @@ $ go run main.go
|
||||||
|
|
||||||
</details>
|
</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.
|
Iris contains extensive and thorough **[wiki](https://github.com/kataras/iris/wiki)** making it easy to get started with the framework.
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,8 @@ func main() {
|
||||||
ctx.Header("Transfer-Encoding", "chunked")
|
ctx.Header("Transfer-Encoding", "chunked")
|
||||||
i := 0
|
i := 0
|
||||||
ints := []int{1, 2, 3, 5, 7, 9, 11, 13, 15, 17, 23, 29}
|
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 {
|
err := ctx.StreamWriter(func(w io.Writer) error {
|
||||||
ctx.Writef("Message number %d<br>", ints[i])
|
ctx.Writef("Message number %d<br>", ints[i])
|
||||||
time.Sleep(500 * time.Millisecond) // simulate delay.
|
time.Sleep(500 * time.Millisecond) // simulate delay.
|
||||||
|
@ -40,20 +41,30 @@ func main() {
|
||||||
Number int `json:"number"`
|
Number int `json:"number"`
|
||||||
}
|
}
|
||||||
|
|
||||||
app.Get("/alternative", func(ctx iris.Context) {
|
app.Get("/json", func(ctx iris.Context) {
|
||||||
ctx.Header("Transfer-Encoding", "chunked")
|
ctx.Header("Transfer-Encoding", "chunked")
|
||||||
i := 0
|
i := 0
|
||||||
ints := []int{1, 2, 3, 5, 7, 9, 11, 13, 15, 17, 23, 29}
|
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 {
|
for {
|
||||||
ctx.JSON(messageNumber{Number: ints[i]})
|
select {
|
||||||
ctx.WriteString("\n")
|
case <-notifyClose:
|
||||||
time.Sleep(500 * time.Millisecond) // simulate delay.
|
// err := ctx.Request().Context().Err()
|
||||||
if i == len(ints)-1 {
|
ctx.Application().Logger().Infof("Connection closed, loop end.")
|
||||||
break
|
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
6
go.mod
|
@ -29,15 +29,15 @@ require (
|
||||||
github.com/ryanuber/columnize v2.1.0+incompatible
|
github.com/ryanuber/columnize v2.1.0+incompatible
|
||||||
github.com/schollz/closestmatch v2.1.0+incompatible
|
github.com/schollz/closestmatch v2.1.0+incompatible
|
||||||
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
|
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
|
github.com/vmihailenco/msgpack/v5 v5.0.0-beta.1
|
||||||
go.etcd.io/bbolt v1.3.5
|
go.etcd.io/bbolt v1.3.5
|
||||||
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
|
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9
|
||||||
golang.org/x/net v0.0.0-20200625001655-4c5254603344
|
golang.org/x/net v0.0.0-20200625001655-4c5254603344
|
||||||
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae
|
golang.org/x/sys v0.0.0-20200625212154-ddb9806d33ae
|
||||||
golang.org/x/text v0.3.3
|
golang.org/x/text v0.3.3
|
||||||
golang.org/x/time v0.0.0-20200416051211-89c76fbcd5d1
|
golang.org/x/time v0.0.0-20200630173020-3af7569d3a1e
|
||||||
google.golang.org/protobuf v1.24.0
|
google.golang.org/protobuf v1.25.0
|
||||||
gopkg.in/ini.v1 v1.57.0
|
gopkg.in/ini.v1 v1.57.0
|
||||||
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
|
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user