-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsse.go
More file actions
75 lines (64 loc) · 1.45 KB
/
sse.go
File metadata and controls
75 lines (64 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package main
import (
"fmt"
"net/http"
"sync"
)
// Broadcaster sends messages to ALL connected clients
type Broadcaster struct {
mu sync.RWMutex
clients map[chan string]bool
}
var broadcast = &Broadcaster{
clients: make(map[chan string]bool),
}
func (b *Broadcaster) Subscribe() chan string {
b.mu.Lock()
defer b.mu.Unlock()
ch := make(chan string, 10)
b.clients[ch] = true
return ch
}
func (b *Broadcaster) Unsubscribe(ch chan string) {
b.mu.Lock()
defer b.mu.Unlock()
delete(b.clients, ch)
close(ch)
}
func (b *Broadcaster) Send(msg string) {
b.mu.RLock()
defer b.mu.RUnlock()
for ch := range b.clients {
select {
case ch <- msg:
default:
// Client buffer full, skip
}
}
}
func handleSSE(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")
w.Header().Set("X-Accel-Buffering", "no")
flusher, ok := w.(http.Flusher)
if !ok {
http.Error(w, "SSE not supported", http.StatusInternalServerError)
return
}
// Subscribe this client
clientChan := broadcast.Subscribe()
defer broadcast.Unsubscribe(clientChan)
// Send initial connection event
fmt.Fprintf(w, "event: connected\ndata: {\"status\":\"connected\"}\n\n")
flusher.Flush()
for {
select {
case <-r.Context().Done():
return
case msg := <-clientChan:
fmt.Fprintf(w, "event: sse-toast\ndata: %s\n\n", msg)
flusher.Flush()
}
}
}