From 56c25c6ab2351d75b5fe38749af5717c30eb0fdc Mon Sep 17 00:00:00 2001 From: Peter Stuifzand Date: Sat, 8 Sep 2018 19:12:53 +0200 Subject: [PATCH] Add too simple events to the server --- cmd/eksterd/microsub.go | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/cmd/eksterd/microsub.go b/cmd/eksterd/microsub.go index 55e726a..a5cca73 100644 --- a/cmd/eksterd/microsub.go +++ b/cmd/eksterd/microsub.go @@ -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" }() - for t := range c { - fmt.Fprintln(w, `event: ping`) - fmt.Fprintf(w, `event: %s\n`, t) - fmt.Fprintln(w) - } + 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.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