mirror of https://github.com/gorilla/websocket.git
Simplify echo example client (#349)
Use existing `done` channel to signal that reader is done instead of closing the connection.
This commit is contained in:
parent
4ac909741d
commit
8fbc40be62
|
@ -38,7 +38,6 @@ func main() {
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
|
|
||||||
go func() {
|
go func() {
|
||||||
defer c.Close()
|
|
||||||
defer close(done)
|
defer close(done)
|
||||||
for {
|
for {
|
||||||
_, message, err := c.ReadMessage()
|
_, message, err := c.ReadMessage()
|
||||||
|
@ -55,6 +54,8 @@ func main() {
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
|
case <-done:
|
||||||
|
return
|
||||||
case t := <-ticker.C:
|
case t := <-ticker.C:
|
||||||
err := c.WriteMessage(websocket.TextMessage, []byte(t.String()))
|
err := c.WriteMessage(websocket.TextMessage, []byte(t.String()))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -63,8 +64,9 @@ func main() {
|
||||||
}
|
}
|
||||||
case <-interrupt:
|
case <-interrupt:
|
||||||
log.Println("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, ""))
|
err := c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("write close:", err)
|
log.Println("write close:", err)
|
||||||
|
@ -74,7 +76,6 @@ func main() {
|
||||||
case <-done:
|
case <-done:
|
||||||
case <-time.After(time.Second):
|
case <-time.After(time.Second):
|
||||||
}
|
}
|
||||||
c.Close()
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue