From 07fd825349e8b7e86f8afb3843cb8e2c2afc7c74 Mon Sep 17 00:00:00 2001 From: Wim Date: Sat, 25 Mar 2017 20:45:10 +0100 Subject: Update vendor --- .../gorilla/websocket/examples/command/main.go | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) (limited to 'vendor/github.com/gorilla/websocket/examples/command') diff --git a/vendor/github.com/gorilla/websocket/examples/command/main.go b/vendor/github.com/gorilla/websocket/examples/command/main.go index f3f022ed..239c5c85 100644 --- a/vendor/github.com/gorilla/websocket/examples/command/main.go +++ b/vendor/github.com/gorilla/websocket/examples/command/main.go @@ -12,16 +12,14 @@ import ( "net/http" "os" "os/exec" - "text/template" "time" "github.com/gorilla/websocket" ) var ( - addr = flag.String("addr", "127.0.0.1:8080", "http service address") - cmdPath string - homeTempl = template.Must(template.ParseFiles("home.html")) + addr = flag.String("addr", "127.0.0.1:8080", "http service address") + cmdPath string ) const ( @@ -36,6 +34,9 @@ const ( // Send pings to peer with this period. Must be less than pongWait. pingPeriod = (pongWait * 9) / 10 + + // Time to wait before force close on connection. + closeGracePeriod = 10 * time.Second ) func pumpStdin(ws *websocket.Conn, w io.Writer) { @@ -57,19 +58,24 @@ func pumpStdin(ws *websocket.Conn, w io.Writer) { func pumpStdout(ws *websocket.Conn, r io.Reader, done chan struct{}) { defer func() { - ws.Close() - close(done) }() s := bufio.NewScanner(r) for s.Scan() { ws.SetWriteDeadline(time.Now().Add(writeWait)) if err := ws.WriteMessage(websocket.TextMessage, s.Bytes()); err != nil { + ws.Close() break } } if s.Err() != nil { log.Println("scan:", s.Err()) } + close(done) + + ws.SetWriteDeadline(time.Now().Add(writeWait)) + ws.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) + time.Sleep(closeGracePeriod) + ws.Close() } func ping(ws *websocket.Conn, done chan struct{}) { @@ -168,8 +174,7 @@ func serveHome(w http.ResponseWriter, r *http.Request) { http.Error(w, "Method not allowed", 405) return } - w.Header().Set("Content-Type", "text/html; charset=utf-8") - homeTempl.Execute(w, r.Host) + http.ServeFile(w, r, "home.html") } func main() { -- cgit v1.2.3