-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventsource.go
80 lines (68 loc) · 1.61 KB
/
eventsource.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
// Simple eventsource handler, for publishing Server-sent events (https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events).
package eventsource
import (
"bytes"
"fmt"
"net/http"
"sync"
)
type Source struct {
lock sync.Mutex
subscribers map[chan []byte]bool
}
// New creates a new source which can be used to publish events
func New() *Source {
return &Source{
subscribers: make(map[chan []byte]bool),
}
}
func (s *Source) subscribe() chan []byte {
ch := make(chan []byte, 100)
s.lock.Lock()
s.subscribers[ch] = true
s.lock.Unlock()
return ch
}
func (s *Source) unsubscribe(ch chan []byte) {
s.lock.Lock()
delete(s.subscribers, ch)
s.lock.Unlock()
}
// Send a message to all connected clients
func (s *Source) Publish(msg []byte) {
msg = bytes.TrimSpace(msg)
msg = bytes.Replace(msg, []byte("\n"), []byte("\ndata: "), -1)
s.lock.Lock()
for ch := range s.subscribers {
select {
case ch <- msg:
default: // Dead or flooded client? discard!
}
}
s.lock.Unlock()
}
// Source implements http.Handler. All connected clients will receive the
// published messages.
func (s *Source) ServeHTTP(w http.ResponseWriter, req *http.Request) {
f, ok := w.(http.Flusher)
if !ok {
http.Error(w, "streaming unsupported", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
ch := s.subscribe()
defer s.unsubscribe(ch)
for {
msg, ok := <-ch
if !ok {
return
}
_, err := fmt.Fprintf(w, "data: %s\n\n", msg)
if err != nil {
return
}
f.Flush()
}
}