Skip to content

Instantly share code, notes, and snippets.

@gperrudin
Created August 20, 2015 16:18
Show Gist options
  • Save gperrudin/f214fbda9aacec1ea5d9 to your computer and use it in GitHub Desktop.
Save gperrudin/f214fbda9aacec1ea5d9 to your computer and use it in GitHub Desktop.
Eventsource golang : how to detect client disconnection ?
func (sh StreamHandler) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
es := eventsource.New(
&eventsource.Settings{
Timeout: 2 * time.Second,
CloseOnTimeout: true,
IdleTimeout: 2 * time.Second,
Gzip: true,
},
func(req *http.Request) [][]byte {
return [][]byte{
[]byte("X-Accel-Buffering: no"),
[]byte("Access-Control-Allow-Origin: *"),
}
},
)
es.ServeHTTP(resp, req)
go func() {
var id int
for {
id++
time.Sleep(1 * time.Second)
es.SendEventMessage("blabla", "message", strconv.Itoa(id))
}
}()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment