refactor: support request cancellation in SSE
that's my first use of select on channels, yay
This commit is contained in:
parent
bb89b8ccf5
commit
4dbbfcd6f2
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue