-
Notifications
You must be signed in to change notification settings - Fork 93
/
ssehub.go
91 lines (77 loc) · 1.88 KB
/
ssehub.go
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package web
import (
"fmt"
"net/http"
"os"
"strings"
)
// SSEHub maintains the set of active clients and broadcasts messages to the clients.
type SSEHub struct {
// Inbound messages from the clients.
Broadcast chan *sse
// Register requests from the clients.
Register chan *sseClient
// Unregister requests from clients.
Unregister chan *sseClient
// Registered clients.
clients map[*sseClient]struct{}
}
type sse struct {
event string
data string
}
func (evt *sse) ClientBytes() []byte {
return []byte(fmt.Sprintf("event: %s\ndata: %s\n\n", evt.event, strings.ReplaceAll(evt.data, "\n", "")))
}
type sseClient struct {
send chan *sse
}
// NewHub creates new SSEHub
func NewHub() *SSEHub {
return &SSEHub{
Broadcast: make(chan *sse),
Register: make(chan *sseClient),
Unregister: make(chan *sseClient),
clients: make(map[*sseClient]struct{}),
}
}
// Run handles communication operations with SSEHub
func (h *SSEHub) Run() {
for {
select {
case client := <-h.Register:
h.clients[client] = struct{}{}
case client := <-h.Unregister:
if _, ok := h.clients[client]; ok {
delete(h.clients, client)
close(client.send)
}
case message := <-h.Broadcast:
for client := range h.clients {
client.send <- message
}
}
}
}
func (h *SSEHub) handler(w http.ResponseWriter, r *http.Request) {
flusher, ok := w.(http.Flusher)
if !ok {
fmt.Fprintf(os.Stderr, "server sent events not supported\n")
return
}
w.Header().Set("Content-Type", "text/event-stream")
client := &sseClient{
send: make(chan *sse),
}
h.Register <- client
// for some reason we need to send some initial data – otherwise following updates are not acknowledged by the browser
_, _ = w.Write((&sse{}).ClientBytes())
flusher.Flush()
for evt := range client.send {
if _, err := w.Write(evt.ClientBytes()); err != nil {
break
}
flusher.Flush()
}
h.Unregister <- client
}