2017-03-24 01:25:00 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-06-29 20:05:12 +02:00
|
|
|
"errors"
|
2017-03-24 01:25:00 +01:00
|
|
|
"io"
|
|
|
|
"time" // showcase the delay
|
|
|
|
|
2019-10-25 00:27:02 +02:00
|
|
|
"github.com/kataras/iris/v12"
|
2017-03-24 01:25:00 +01:00
|
|
|
)
|
|
|
|
|
2020-06-29 20:05:12 +02:00
|
|
|
var errDone = errors.New("done")
|
|
|
|
|
2017-03-24 01:25:00 +01:00
|
|
|
func main() {
|
|
|
|
app := iris.New()
|
|
|
|
|
2017-08-27 19:35:23 +02:00
|
|
|
app.Get("/", func(ctx iris.Context) {
|
2018-04-21 19:46:16 +02:00
|
|
|
ctx.ContentType("text/html")
|
|
|
|
ctx.Header("Transfer-Encoding", "chunked")
|
2017-03-24 01:25:00 +01:00
|
|
|
i := 0
|
2018-04-21 19:46:16 +02:00
|
|
|
ints := []int{1, 2, 3, 5, 7, 9, 11, 13, 15, 17, 23, 29}
|
2020-07-01 13:43:24 +02:00
|
|
|
// Send the response in chunks and wait for half a second between each chunk,
|
|
|
|
// until connection close.
|
2020-06-29 20:05:12 +02:00
|
|
|
err := ctx.StreamWriter(func(w io.Writer) error {
|
|
|
|
ctx.Writef("Message number %d<br>", ints[i])
|
2018-04-21 19:46:16 +02:00
|
|
|
time.Sleep(500 * time.Millisecond) // simulate delay.
|
|
|
|
if i == len(ints)-1 {
|
2020-06-29 20:05:12 +02:00
|
|
|
return errDone // ends the loop.
|
2017-03-24 01:25:00 +01:00
|
|
|
}
|
2018-04-21 19:46:16 +02:00
|
|
|
i++
|
2020-06-29 20:05:12 +02:00
|
|
|
return nil // continue write
|
2017-03-24 01:25:00 +01:00
|
|
|
})
|
2020-06-29 20:05:12 +02:00
|
|
|
|
|
|
|
if err != errDone {
|
|
|
|
// Test it by canceling the request before the stream ends:
|
|
|
|
// [ERRO] $DATETIME stream: context canceled.
|
|
|
|
ctx.Application().Logger().Errorf("stream: %v", err)
|
|
|
|
}
|
2017-03-24 01:25:00 +01:00
|
|
|
})
|
|
|
|
|
2018-04-21 19:46:16 +02:00
|
|
|
type messageNumber struct {
|
|
|
|
Number int `json:"number"`
|
|
|
|
}
|
|
|
|
|
2020-07-01 13:43:24 +02:00
|
|
|
app.Get("/json", func(ctx iris.Context) {
|
2018-04-21 19:46:16 +02:00
|
|
|
ctx.Header("Transfer-Encoding", "chunked")
|
|
|
|
i := 0
|
|
|
|
ints := []int{1, 2, 3, 5, 7, 9, 11, 13, 15, 17, 23, 29}
|
2020-07-01 13:43:24 +02:00
|
|
|
// Send the response in chunks and wait for half a second between each chunk,
|
|
|
|
// until connection close.
|
|
|
|
notifyClose := ctx.Request().Context().Done()
|
2018-04-21 19:46:16 +02:00
|
|
|
for {
|
2020-07-01 13:43:24 +02:00
|
|
|
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()
|
2017-03-24 01:25:00 +01:00
|
|
|
}
|
2018-04-21 19:46:16 +02:00
|
|
|
}
|
2017-03-24 01:25:00 +01:00
|
|
|
})
|
|
|
|
|
2020-03-05 21:41:27 +01:00
|
|
|
app.Listen(":8080")
|
2017-03-24 01:25:00 +01:00
|
|
|
}
|
2020-06-29 20:05:12 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
Look the following methods too:
|
|
|
|
- Context.OnClose(callback)
|
|
|
|
- Context.OnConnectionClose(callback) and
|
|
|
|
- Context.Request().Context().Done()/.Err() too
|
|
|
|
*/
|