Skip to content

Commit

Permalink
Simplify echo example client (#349)
Browse files Browse the repository at this point in the history
Use existing `done` channel to signal that reader is done instead of closing the connection.
  • Loading branch information
claudia-jones authored and garyburd committed Feb 19, 2018
1 parent 4ac9097 commit 8fbc40b
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions examples/echo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func main() {
done := make(chan struct{})

go func() {
defer c.Close()
defer close(done)
for {
_, message, err := c.ReadMessage()
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -74,7 +76,6 @@ func main() {
case <-done:
case <-time.After(time.Second):
}
c.Close()
return
}
}
Expand Down

0 comments on commit 8fbc40b

Please sign in to comment.