From 884766dc72bd81af03f6a086ef70bd6e3196e3e1 Mon Sep 17 00:00:00 2001 From: Steve Date: Thu, 29 Jul 2021 17:40:11 -0400 Subject: [PATCH] opt into polling, clients no longer crash on leave --- cmd/client2/backend.go | 27 +++++++++++++++++---------- cmd/server/server.go | 2 ++ internal/coordinator/coordinator.go | 7 ++++++- internal/coordinator/session.go | 2 -- 4 files changed, 25 insertions(+), 13 deletions(-) diff --git a/cmd/client2/backend.go b/cmd/client2/backend.go index 64e9eae..21b4d88 100644 --- a/cmd/client2/backend.go +++ b/cmd/client2/backend.go @@ -28,6 +28,7 @@ func receiveHandler(connection *websocket.Conn, container *UIContainer) { err := connection.ReadJSON(&resp) if err != nil { log.Println("Error in receive:", err) + break } switch resp.Result { case coordinator.SessionRespJoined1: @@ -55,7 +56,8 @@ func receiveHandler(connection *websocket.Conn, container *UIContainer) { } case coordinator.SessionRespLeft: container.Output <- "game left" - break + matchID = uuid.Nil + return case coordinator.SessionRespBroadcastSenTurn: container.Output <- "Sentinal may take their turn" case coordinator.SessionRespBroadcastScoTrun: @@ -166,15 +168,20 @@ func backend(container *UIContainer) { } return case <-ticker.C: - if matchID != uuid.Nil { - err := conn.WriteJSON(coordinator.SessionCommand{ - ID: id, - MatchID: matchID, - Command: coordinator.SessionCmdPoll, - }) - if err != nil { - log.Println("Error writing to websocket:", err) - return + select { + case <-done: + return + default: + if matchID != uuid.Nil { + err := conn.WriteJSON(coordinator.SessionCommand{ + ID: id, + MatchID: matchID, + Command: coordinator.SessionCmdPoll, + }) + if err != nil { + log.Println("Error writing to websocket:", err) + return + } } } } diff --git a/cmd/server/server.go b/cmd/server/server.go index 749a780..fa87df2 100644 --- a/cmd/server/server.go +++ b/cmd/server/server.go @@ -3,6 +3,7 @@ package main import ( "log" "net/http" + "time" "git.saintnet.tech/stryan/snengame/internal/coordinator" "github.com/gorilla/websocket" @@ -53,6 +54,7 @@ func serveWs(c *coordinator.Coordinator, w http.ResponseWriter, r *http.Request) break } if resp.Result == coordinator.SessionRespLeft { + time.Sleep(1 * time.Second) //give clients a second to respond break } } diff --git a/internal/coordinator/coordinator.go b/internal/coordinator/coordinator.go index badb966..a46a1ee 100644 --- a/internal/coordinator/coordinator.go +++ b/internal/coordinator/coordinator.go @@ -122,6 +122,11 @@ func (c *Coordinator) Coordinate(cmd *SessionCommand) *SessionCommandResult { case SessionCmdPoll: m, exists := c.Matches[cmd.MatchID] if exists { + _, exists = m.Broadcasts[cmd.ID] + if !exists { + log.Printf("%v has opted in to polling", cmd.ID) + m.Broadcasts[cmd.ID] = make(chan SessionResp, 10) + } select { case res := <-m.Broadcasts[cmd.ID]: return &SessionCommandResult{ @@ -158,7 +163,7 @@ func MatchWatcher(m *Session) { for { select { case <-ticker.C: - if m.Active && m.Game != nil { + if m.Active && m.Game != nil && len(m.Broadcasts) > 0 { if m.Game.Status == game.StatusLobby && !sen_ready && m.Game.SentinalPlayer.Ready { for _, v := range m.Broadcasts { v <- SessionRespBroadcastSenReady diff --git a/internal/coordinator/session.go b/internal/coordinator/session.go index 9978f5f..b11250e 100644 --- a/internal/coordinator/session.go +++ b/internal/coordinator/session.go @@ -71,7 +71,6 @@ func (s *Session) Join(id uuid.UUID) SessionResp { for _, v := range s.Broadcasts { v <- SessionRespBroadcastSenJoin } - s.Broadcasts[id] = make(chan SessionResp) return SessionRespJoined1 } else if s.p2 == uuid.Nil { s.p2 = id @@ -79,7 +78,6 @@ func (s *Session) Join(id uuid.UUID) SessionResp { for _, v := range s.Broadcasts { v <- SessionRespBroadcastScoJoin } - s.Broadcasts[id] = make(chan SessionResp) return SessionRespJoined2 } else { return SessionRespJoinError