aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--backend/.golangci.yml6
-rw-r--r--backend/admin/handlers.go5
-rw-r--r--backend/game/client.go64
-rw-r--r--backend/game/ws.go21
-rw-r--r--backend/main.go4
5 files changed, 64 insertions, 36 deletions
diff --git a/backend/.golangci.yml b/backend/.golangci.yml
index 7832fa2..a8eddf8 100644
--- a/backend/.golangci.yml
+++ b/backend/.golangci.yml
@@ -1,10 +1,4 @@
linters:
- disable-all: true
enable:
- gofmt
- goimports
- - gosimple
- - govet
- - ineffassign
- - staticcheck
- - unused
diff --git a/backend/admin/handlers.go b/backend/admin/handlers.go
index d445de8..2a678d3 100644
--- a/backend/admin/handlers.go
+++ b/backend/admin/handlers.go
@@ -244,7 +244,10 @@ func (h *AdminHandler) postGameEdit(c echo.Context) error {
{
// TODO:
if state != row.State && state == "prepare" {
- h.hubs.StartGame(int(gameID))
+ err := h.hubs.StartGame(int(gameID))
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
+ }
}
}
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
}
}
}
diff --git a/backend/game/ws.go b/backend/game/ws.go
index 9a3956b..8e219d6 100644
--- a/backend/game/ws.go
+++ b/backend/game/ws.go
@@ -1,6 +1,7 @@
package game
import (
+ "log"
"net/http"
"time"
@@ -36,8 +37,14 @@ func servePlayerWs(hub *gameHub, w http.ResponseWriter, r *http.Request, playerI
}
hub.registerPlayer <- player
- go player.writePump()
- go player.readPump()
+ go func() {
+ err := player.writePump()
+ log.Printf("%v", err)
+ }()
+ go func() {
+ err := player.readPump()
+ log.Printf("%v", err)
+ }()
return nil
}
@@ -53,7 +60,13 @@ func serveWatcherWs(hub *gameHub, w http.ResponseWriter, r *http.Request) error
}
hub.registerWatcher <- watcher
- go watcher.writePump()
- go watcher.readPump()
+ go func() {
+ err := watcher.writePump()
+ log.Printf("%v", err)
+ }()
+ go func() {
+ err := watcher.readPump()
+ log.Printf("%v", err)
+ }()
return nil
}
diff --git a/backend/main.go b/backend/main.go
index 62499d6..9f92415 100644
--- a/backend/main.go
+++ b/backend/main.go
@@ -99,7 +99,9 @@ func main() {
go gameHubs.Run()
go func() {
- workerServer.Run()
+ if err := workerServer.Run(); err != nil {
+ log.Fatal(err)
+ }
}()
if err := e.Start(":80"); err != http.ErrServerClosed {