-
Notifications
You must be signed in to change notification settings - Fork 3
/
eventSource.go
56 lines (42 loc) · 1.2 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
/*
Package eventsouce provides a basic interface for
serving server sent events (aka EventSource).
It is patterned after the Go Websocket library:
https://code.google.com/p/go/source/browse?repo=net#hg%2Fwebsocket
For more destails about Server Sent Events see:
http://html5doctor.com/server-sent-events/
http://www.html5rocks.com/en/tutorials/eventsource/basics/
http://cjihrig.com/blog/the-server-side-of-server-sent-events/
*/
package eventsource
import (
"fmt"
"net/http"
)
type Conn struct {
writer http.ResponseWriter
http.Flusher
http.CloseNotifier
}
func (c Conn) Write(message string) {
c.writer.Write([]byte(fmt.Sprintf("data: %s", message)))
c.writer.Write([]byte("\n\n"))
c.Flush()
}
type Handler func(*Conn)
func (h Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
f, f_ok := w.(http.Flusher)
if !f_ok {
panic("ResponseWriter is not a Flusher")
}
cn, cn_ok := w.(http.CloseNotifier)
if !cn_ok {
panic("ResponseWriter is not a CloseNotifier")
}
w.Header().Set("Content-Type", "text/event-stream")
w.Header().Set("Cache-Control", "no-cache")
w.Header().Set("Connection", "keep-alive")
w.Header().Set("Transfer-Encoding", "chunked")
f.Flush()
h(&Conn{w, f, cn})
}