Skip to content

Commit 1e992ab

Browse files
committed
notes on streaming http servers
1 parent 32d43de commit 1e992ab

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"net/http"
6+
"time"
7+
)
8+
9+
func stream(resp http.ResponseWriter, req *http.Request) {
10+
respf, ok := resp.(http.Flusher)
11+
if !ok {
12+
panic("not flushable")
13+
}
14+
fmt.Println("stream")
15+
resp.WriteHeader(200)
16+
for i := 0; i < 10; i++ {
17+
n, err := resp.Write([]byte("tick\n"))
18+
respf.Flush()
19+
fmt.Println("tick", n, err)
20+
time.Sleep(time.Second * 1)
21+
}
22+
}
23+
24+
func main() {
25+
http.HandleFunc("/", stream)
26+
fmt.Println("serve")
27+
http.ListenAndServe(":5000", nil)
28+
}

0 commit comments

Comments
 (0)