diff options
Diffstat (limited to 'backend/game/client.go')
| -rw-r--r-- | backend/game/client.go | 64 |
1 files changed, 40 insertions, 24 deletions
diff --git a/backend/game/client.go b/backend/game/client.go index fa699ce..5bcb98f 100644 --- a/backend/game/client.go +++ b/backend/game/client.go @@ -2,6 +2,7 @@ package game import ( "encoding/json" + "fmt" "log" "time" @@ -16,32 +17,32 @@ type playerClient struct { } // Receives messages from the client and sends them to the hub. -func (c *playerClient) readPump() { +func (c *playerClient) readPump() error { defer func() { log.Printf("closing player client") c.hub.unregisterPlayer <- c c.conn.Close() }() c.conn.SetReadLimit(maxMessageSize) - c.conn.SetReadDeadline(time.Now().Add(pongWait)) - c.conn.SetPongHandler(func(string) error { c.conn.SetReadDeadline(time.Now().Add(pongWait)); return nil }) + if err := c.conn.SetReadDeadline(time.Now().Add(pongWait)); err != nil { + return err + } + c.conn.SetPongHandler(func(string) error { return c.conn.SetReadDeadline(time.Now().Add(pongWait)) }) for { var rawMessage map[string]json.RawMessage if err := c.conn.ReadJSON(&rawMessage); err != nil { - log.Printf("error: %v", err) - return + return err } message, err := asPlayerMessageC2S(rawMessage) if err != nil { - log.Printf("error: %v", err) - return + return err } c.hub.playerC2SMessages <- &playerMessageC2SWithClient{c, message} } } // Receives messages from the hub and sends them to the client. -func (c *playerClient) writePump() { +func (c *playerClient) writePump() error { ticker := time.NewTicker(pingPeriod) defer func() { ticker.Stop() @@ -50,20 +51,26 @@ func (c *playerClient) writePump() { for { select { case message, ok := <-c.s2cMessages: - c.conn.SetWriteDeadline(time.Now().Add(writeWait)) + if err := c.conn.SetWriteDeadline(time.Now().Add(writeWait)); err != nil { + return err + } if !ok { - c.conn.WriteMessage(websocket.CloseMessage, []byte{}) - return + if err := c.conn.WriteMessage(websocket.CloseMessage, []byte{}); err != nil { + return err + } + return fmt.Errorf("closing player client") } err := c.conn.WriteJSON(message) if err != nil { - return + return err } case <-ticker.C: - c.conn.SetWriteDeadline(time.Now().Add(writeWait)) + if err := c.conn.SetWriteDeadline(time.Now().Add(writeWait)); err != nil { + return err + } if err := c.conn.WriteMessage(websocket.PingMessage, nil); err != nil { - return + return err } } } @@ -76,14 +83,17 @@ type watcherClient struct { } // Receives messages from the client and sends them to the hub. -func (c *watcherClient) readPump() { +func (c *watcherClient) readPump() error { c.conn.SetReadLimit(maxMessageSize) - c.conn.SetReadDeadline(time.Now().Add(pongWait)) - c.conn.SetPongHandler(func(string) error { c.conn.SetReadDeadline(time.Now().Add(pongWait)); return nil }) + if err := c.conn.SetReadDeadline(time.Now().Add(pongWait)); err != nil { + return err + } + c.conn.SetPongHandler(func(string) error { return c.conn.SetReadDeadline(time.Now().Add(pongWait)) }) + return nil } // Receives messages from the hub and sends them to the client. -func (c *watcherClient) writePump() { +func (c *watcherClient) writePump() error { ticker := time.NewTicker(pingPeriod) defer func() { ticker.Stop() @@ -94,20 +104,26 @@ func (c *watcherClient) writePump() { for { select { case message, ok := <-c.s2cMessages: - c.conn.SetWriteDeadline(time.Now().Add(writeWait)) + if err := c.conn.SetWriteDeadline(time.Now().Add(writeWait)); err != nil { + return err + } if !ok { - c.conn.WriteMessage(websocket.CloseMessage, []byte{}) - return + if err := c.conn.WriteMessage(websocket.CloseMessage, []byte{}); err != nil { + return err + } + return fmt.Errorf("closing watcher client") } err := c.conn.WriteJSON(message) if err != nil { - return + return err } case <-ticker.C: - c.conn.SetWriteDeadline(time.Now().Add(writeWait)) + if err := c.conn.SetWriteDeadline(time.Now().Add(writeWait)); err != nil { + return err + } if err := c.conn.WriteMessage(websocket.PingMessage, nil); err != nil { - return + return err } } } |
