代码拉取完成,页面将自动刷新
package main
import (
"fmt" // just an optional helper
"io"
"time" // showcase the delay
"github.com/kataras/iris"
)
func main() {
app := iris.New()
timeWaitForCloseStream := 4 * time.Second
app.Get("/", func(ctx iris.Context) {
i := 0
// goroutine in order to no block and just wait,
// goroutine is OPTIONAL and not a very good option but it depends on the needs
// Look the streaming_simple_2 for an alternative code style
// Send the response in chunks and wait for a second between each chunk.
go ctx.StreamWriter(func(w io.Writer) bool {
i++
fmt.Fprintf(w, "this is a message number %d\n", i) // write
time.Sleep(time.Second) // imaginary delay
if i == 4 {
return false // close and flush
}
return true // continue write
})
// when this handler finished the client should be see the stream writer's contents
// simulate a job here...
time.Sleep(timeWaitForCloseStream)
})
app.Get("/alternative", func(ctx iris.Context) {
// Send the response in chunks and wait for a second between each chunk.
ctx.StreamWriter(func(w io.Writer) bool {
for i := 1; i <= 4; i++ {
fmt.Fprintf(w, "this is a message number %d\n", i) // write
time.Sleep(time.Second)
}
// when this handler finished the client should be see the stream writer's contents
return false // stop and flush the contents
})
})
app.Run(iris.Addr(":8080"))
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。