forked from antage/eventsource
/
consumer.go
67 lines (58 loc) · 1.2 KB
/
consumer.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
package eventsource
import (
"log"
"net/http"
"strconv"
)
func lastEventId(r *http.Request) (int, error) {
h := r.Header.Get("Last-Event-ID")
if len(h) > 0 {
return strconv.Atoi(h)
} else {
return -1, nil
}
}
func consumerHandler(w http.ResponseWriter, r *http.Request, es *eventSource) {
if r.Header.Get("Accept") != "text/event-stream" {
http.Error(w, "eventsource: should accept text/event-stream", http.StatusNotAcceptable)
return
}
var (
fl http.Flusher
con *consumer
id int
err error
ok bool
)
id, err = lastEventId(r)
if err != nil {
http.Error(w, "eventsource: bad last-event-id header", http.StatusBadRequest)
return
}
fl, ok = w.(http.Flusher)
if !ok {
http.Error(w, "eventsouce: response does not implement http.Flusher", http.StatusInternalServerError)
return
}
h := w.Header()
h.Set("Content-Type", "text/event-stream")
h.Set("Cache-Control", "no-cache")
h.Set("Connection", "keep-alive")
con = es.Subscribe(id)
defer es.Unsubscribe(con)
for {
select {
case b := <-con.ch:
if _, err := w.Write(b); err == nil {
fl.Flush()
} else {
log.Print(err)
return
}
case <-con.quit:
close(con.ch)
close(con.quit)
return
}
}
}