mirror of
https://github.com/m1k1o/neko-rooms.git
synced 2026-11-02 22:51:34 -06:00
room events optional sse.
This commit is contained in:
+13
-2
@@ -252,10 +252,16 @@ func (manager *ApiManagerCtx) roomGenericAction(action func(ctx context.Context,
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (manager *ApiManagerCtx) events(w http.ResponseWriter, r *http.Request) {
|
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("Cache-Control", "no-cache")
|
||||||
w.Header().Set("Connection", "keep-alive")
|
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)
|
flusher, ok := w.(http.Flusher)
|
||||||
if !ok {
|
if !ok {
|
||||||
http.Error(w, "Connection does not support streaming", http.StatusBadRequest)
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintf(w, "data: %s\n\n", jsonData)
|
if sse {
|
||||||
|
fmt.Fprintf(w, "data: %s\n\n", jsonData)
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(w, "%s\n", jsonData)
|
||||||
|
}
|
||||||
|
|
||||||
flusher.Flush()
|
flusher.Flush()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user