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 (#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

1 comment on commit 893c6ca

@daihuynh
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe the logic for detecting client disconnection is wrong because "case <- clientGone" should return false so as to stop the stream.
I've just found this issue by today while trying to implement an SSE API in my server.

Moreover, in the realtime-chat example, the author reimplemented clientGone again in his "func stream"
https://github.com/gin-gonic/examples/blob/master/realtime-chat/main.go

Please sign in to comment.