Skip to content

Instantly share code, notes, and snippets.

@allada
Created July 21, 2016 21:30
Show Gist options
  • Save allada/080035ff7e75d1d882f6fe3af41d1146 to your computer and use it in GitHub Desktop.
Save allada/080035ff7e75d1d882f6fe3af41d1146 to your computer and use it in GitHub Desktop.
package main
import (
"net/http"
"io"
"time"
"golang.org/x/net/websocket"
"golang.org/x/net/http2"
)
func echoHandler(ws *websocket.Conn) {
io.Copy(ws, ws)
}
func main() {
// Wait a few seconds before empty script is served
http.HandleFunc("/waitScript", scriptfile)
http.HandleFunc("/bufferSend", bufferSend)
http.HandleFunc("/chunkData", chunkData)
http.HandleFunc("/h2Receive1", h2Receive)
http.HandleFunc("/h2Receive2", h2Receive)
http.HandleFunc("/h2Receive3", h2Receive)
// Websocket echo
http.Handle("/wsEcho", websocket.Handler(echoHandler))
go func() {
// Serve all files in this directory
srv := &http.Server{
Addr: ":8081", // Normally ":443"
}
http2.ConfigureServer(srv, &http2.Server{})
srv.ListenAndServeTLS("server.crt", "server.key")
}()
http.ListenAndServe(":8080", nil)
}
func scriptfile(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/javascript")
time.Sleep(3 * time.Second)
}
func bufferSend(w http.ResponseWriter, r *http.Request) {
flusher := w.(http.Flusher)
w.Header().Set("Content-Type", "application/json")
w.Header().Set("Content-Length", "2")
w.WriteHeader(http.StatusOK)
//flusher.Flush()
//time.Sleep(1 * time.Second)
//flusher.Flush()
w.Write([]byte{'a'})
flusher.Flush()
time.Sleep(1 * time.Second)
w.Write([]byte{'a'})
flusher.Flush()
time.Sleep(1 * time.Second)
}
func h2Receive(w http.ResponseWriter, r *http.Request) {
}
func chunkData(w http.ResponseWriter, r *http.Request) {
flusher := w.(http.Flusher)
w.Header().Add("Link", "</h2Receive1>; rel=preload;")
w.Header().Add("Link", "</h2Receive2>; rel=preload;")
w.Header().Add("Link", "</h2Receive3>; rel=preload;")
//w.Header().Set("Transfer-Encoding", "chunked")
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusOK)
flusher.Flush()
time.Sleep(1 * time.Second)
flusher.Flush()
w.Write([]byte{'a'})
flusher.Flush()
time.Sleep(1 * time.Second)
w.Write([]byte{'a'})
flusher.Flush()
time.Sleep(1 * time.Second)
w.Write([]byte{'a'})
flusher.Flush()
time.Sleep(1 * time.Second)
//w.Write([]byte{'1', '\r', '\n', 'a', '\r', '\n'})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment