Add too simple events to the server
All checks were successful
the build was successful

This commit is contained in:
Peter Stuifzand 2018-09-08 19:12:53 +02:00
parent 0d1d391f2a
commit 56c25c6ab2
Signed by: peter
GPG Key ID: 374322D56E5209E8

View File

@ -135,7 +135,7 @@ func (h *microsubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
} else if action == "events" {
w.Header().Add("Content-Type", "text/event-stream")
//w.Header().Add("Content-Type", "text/event-stream")
c := make(chan string)
go func() {
@ -150,11 +150,21 @@ func (h *microsubHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
c <- "end"
}()
conn, _, _ := w.(http.Hijacker).Hijack()
fmt.Fprint(conn, "HTTP/1.0 200 OK\r\n")
fmt.Fprint(conn, "Content-Type: text/event-stream\r\n")
fmt.Fprint(conn, "Access-Control-Allow-Origin: *\r\n")
fmt.Fprint(conn, "\r\n")
go func() {
for t := range c {
fmt.Fprintln(w, `event: ping`)
fmt.Fprintf(w, `event: %s\n`, t)
fmt.Fprintln(w)
fmt.Fprint(conn, `event: ping`)
fmt.Fprint(conn, "\r\n")
fmt.Fprintf(conn, `data: %s`, t)
fmt.Fprint(conn, "\r\n")
fmt.Fprint(conn, "\r\n")
}
conn.Close()
}()
} else {
http.Error(w, fmt.Sprintf("unknown action %s\n", action), 500)
return