Created
June 12, 2020 09:12
-
-
Save tbruyelle/537adf359aa681128e922a5a4ab5961b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"net/http" | |
"time" | |
) | |
func stream() { | |
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { | |
ticker := time.NewTicker(time.Second * 3) | |
defer ticker.Stop() | |
var count int | |
//fmt.Fprintf(w, "# ~1KB of junk to force browsers to start rendering immediately: \n") | |
//io.WriteString(w, strings.Repeat("# xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n", 13)) | |
for { | |
fmt.Println("streaming", count) | |
fmt.Fprintf(w, "{\"key\": %d}\n", count) | |
count++ | |
w.(http.Flusher).Flush() | |
select { | |
case <-ticker.C: | |
case <-r.Context().Done(): | |
fmt.Println("done:", r.Context().Err()) | |
return | |
} | |
} | |
}) | |
//srv := http.Server{ | |
// Addr: ":8181", | |
//} | |
//http2.ConfigureServer(&srv, nil) | |
//log.Fatal(srv.ListenAndServe()) | |
log.Fatal(http.ListenAndServe(":8181", nil)) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment