Refactor
This commit is contained in:
+10
-5
@@ -9,17 +9,17 @@ import (
|
||||
)
|
||||
|
||||
func handleSSE(w http.ResponseWriter, r *http.Request) {
|
||||
roomID := getPathInt(r, "room_id")
|
||||
roomID, err := getPathInt(r, "room_id")
|
||||
if err != nil {
|
||||
http.Error(w, "invalid room id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
user, ok := r.Context().Value(userKey).(*lib.User)
|
||||
if !ok {
|
||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
ch := make(chan string, 10)
|
||||
addSSEClient(roomID, user.ID, ch)
|
||||
broadcast(roomID, "members")
|
||||
|
||||
w.Header().Set("Content-Type", "text/event-stream")
|
||||
w.Header().Set("Cache-Control", "no-cache")
|
||||
w.Header().Set("Connection", "keep-alive")
|
||||
@@ -30,6 +30,11 @@ func handleSSE(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "streaming unsupported", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
ch := make(chan string, 10)
|
||||
addSSEClient(roomID, user.ID, ch)
|
||||
broadcast(roomID, "members")
|
||||
|
||||
notify := r.Context().Done()
|
||||
heartbeat := time.NewTicker(25 * time.Second)
|
||||
defer heartbeat.Stop()
|
||||
|
||||
Reference in New Issue
Block a user