refactor: support request cancellation in SSE

that's my first use of select on channels, yay
This commit is contained in:
efim 2023-11-05 15:46:07 +00:00
parent bb89b8ccf5
commit 4dbbfcd6f2
1 changed files with 14 additions and 8 deletions

View File

@ -37,17 +37,23 @@ func streamingBsRoute() http.HandlerFunc {
w.Header().Set("Content-Type", "text/event-stream")
startTime, endTime := 0, 0
for {
select {
case <-r.Context().Done():
log.Printf("canlecced streaming!")
return
default:
log.Printf("another step in streaming bs")
data := "data: <div>hello with data %d! waited %d</div> \n\n"
startTime = time.Now().Nanosecond()
diff := endTime - startTime
fmt.Fprintf(w, data, rand.Intn(100), diff)
w.(http.Flusher).Flush()
time.Sleep(10 * time.Second)
time.Sleep(3 * time.Second)
endTime = time.Now().Nanosecond()
}
}
}
}
func roomPageRoute(
templateFs *embed.FS,