mirror of https://github.com/gin-gonic/gin.git
Added stream flag indicates if client disconnected in middle of streaming (#1252)
This commit is contained in:
parent
3b84a430d0
commit
893c6cae07
|
@ -896,19 +896,20 @@ func (c *Context) SSEvent(name string, message interface{}) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// Stream sends a streaming response.
|
// Stream sends a streaming response and returns a boolean
|
||||||
func (c *Context) Stream(step func(w io.Writer) bool) {
|
// indicates "Is client disconnected in middle of stream"
|
||||||
|
func (c *Context) Stream(step func(w io.Writer) bool) bool {
|
||||||
w := c.Writer
|
w := c.Writer
|
||||||
clientGone := w.CloseNotify()
|
clientGone := w.CloseNotify()
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-clientGone:
|
case <-clientGone:
|
||||||
return
|
return true
|
||||||
default:
|
default:
|
||||||
keepOpen := step(w)
|
keepOpen := step(w)
|
||||||
w.Flush()
|
w.Flush()
|
||||||
if !keepOpen {
|
if !keepOpen {
|
||||||
return
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue