func HandleSSE(w http.ResponseWriter, r *http.Request) { conn, err := sse.Upgrade(w, r) if err != nil { fmt.Printf("Error occured: %q", err.Error()) } getUserLocation(conn) for { updateCurrentWeather(conn) time.Sleep(60 * time.Second) // Updates every 60 seconds } }
func HandleSSE(w http.ResponseWriter, r *http.Request) { conn, err := sse.Upgrade(w, r) if err != nil { // log error to console fmt.Printf("Error occured: %q", err.Error()) } for { // Trigger event "time" with current time conn.WriteStringEvent("time", time.Now().Format("Mon Jan 2 15:04:05 MST 2006")) conn.WriteStringEvent("feed", "User XY did Z") time.Sleep(1 * time.Second) } }
func HandleSSE(w http.ResponseWriter, r *http.Request) { conn, err := sse.Upgrade(w, r) if err != nil { // log error to console fmt.Printf("Error occured: %q", err.Error()) } for { // update time every second if conn.WriteString(time.Now().Format("Mon Jan 2 15:04:05 MST 2006")) != nil { // stop once an error occurs return } time.Sleep(1 * time.Second) } }
func vkvWatchKeyHandler(vkvhub *hub.Hub) func(http.ResponseWriter, *http.Request) { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r) defer func() { log.Printf("WATCH CLOSE") }() conn, _ := sse.Upgrade(w, r) notify := w.(http.CloseNotifier).CloseNotify() stream := vkvhub.Sub(vars["key"]) L: for { select { case <-notify: log.Printf("Close") vkvhub.Unsub(vars["key"], stream) break L case m := <-stream: conn.WriteString(m) } } } }