From 8fbc40be62b3d1153cd326abb908db7737715036 Mon Sep 17 00:00:00 2001 From: claudia-jones <36607057+claudia-jones@users.noreply.github.com> Date: Sun, 18 Feb 2018 16:00:50 -0800 Subject: [PATCH] Simplify echo example client (#349) Use existing `done` channel to signal that reader is done instead of closing the connection. --- examples/echo/client.go | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/examples/echo/client.go b/examples/echo/client.go index 6578094..bf0e657 100644 --- a/examples/echo/client.go +++ b/examples/echo/client.go @@ -38,7 +38,6 @@ func main() { done := make(chan struct{}) go func() { - defer c.Close() defer close(done) for { _, message, err := c.ReadMessage() @@ -55,6 +54,8 @@ func main() { for { select { + case <-done: + return case t := <-ticker.C: err := c.WriteMessage(websocket.TextMessage, []byte(t.String())) if err != nil { @@ -63,8 +64,9 @@ func main() { } case <-interrupt: log.Println("interrupt") - // To cleanly close a connection, a client should send a close - // frame and wait for the server to close the connection. + + // Cleanly close the connection by sending a close message and then + // waiting (with timeout) for the server to close the connection. err := c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) if err != nil { log.Println("write close:", err) @@ -74,7 +76,6 @@ func main() { case <-done: case <-time.After(time.Second): } - c.Close() return } }