Created
August 20, 2015 16:18
-
-
Save gperrudin/f214fbda9aacec1ea5d9 to your computer and use it in GitHub Desktop.
Eventsource golang : how to detect client disconnection ?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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