notes on streaming http servers
This commit is contained in:
parent
32d43de509
commit
1e992ab31d
28
examples/streaming-http-servers/streaming-http-servers.go
Normal file
28
examples/streaming-http-servers/streaming-http-servers.go
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func stream(resp http.ResponseWriter, req *http.Request) {
|
||||||
|
respf, ok := resp.(http.Flusher)
|
||||||
|
if !ok {
|
||||||
|
panic("not flushable")
|
||||||
|
}
|
||||||
|
fmt.Println("stream")
|
||||||
|
resp.WriteHeader(200)
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
n, err := resp.Write([]byte("tick\n"))
|
||||||
|
respf.Flush()
|
||||||
|
fmt.Println("tick", n, err)
|
||||||
|
time.Sleep(time.Second * 1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
http.HandleFunc("/", stream)
|
||||||
|
fmt.Println("serve")
|
||||||
|
http.ListenAndServe(":5000", nil)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user