room events optional sse.

This commit is contained in:
Miroslav Šedivý
2023-12-02 00:13:47 +01:00
parent 2f7aa7ed13
commit d7f7acc7f3
+12 -1
View File
@@ -252,10 +252,16 @@ func (manager *ApiManagerCtx) roomGenericAction(action func(ctx context.Context,
}
func (manager *ApiManagerCtx) events(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
sse := r.URL.Query().Has("sse")
if sse {
w.Header().Set("Content-Type", "text/event-stream")
} else {
w.Header().Set("Content-Type", "application/json")
}
flusher, ok := w.(http.Flusher)
if !ok {
http.Error(w, "Connection does not support streaming", http.StatusBadRequest)
@@ -277,7 +283,12 @@ func (manager *ApiManagerCtx) events(w http.ResponseWriter, r *http.Request) {
return
}
if sse {
fmt.Fprintf(w, "data: %s\n\n", jsonData)
} else {
fmt.Fprintf(w, "%s\n", jsonData)
}
flusher.Flush()
}
}