Skip to content

Commit

Permalink
Added stream flag indicates if client disconnected in middle of strea…
Browse files Browse the repository at this point in the history
…ming (gin-gonic#1252)
  • Loading branch information
KromDaniel authored and thinkerou committed Mar 2, 2019
1 parent 3b84a43 commit 893c6ca
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -896,19 +896,20 @@ func (c *Context) SSEvent(name string, message interface{}) {
})
}

// Stream sends a streaming response.
func (c *Context) Stream(step func(w io.Writer) bool) {
// Stream sends a streaming response and returns a boolean
// indicates "Is client disconnected in middle of stream"
func (c *Context) Stream(step func(w io.Writer) bool) bool {
w := c.Writer
clientGone := w.CloseNotify()
for {
select {
case <-clientGone:
return
return true
default:
keepOpen := step(w)
w.Flush()
if !keepOpen {
return
return false
}
}
}
Expand Down

0 comments on commit 893c6ca

Please sign in to comment.