opt into polling, clients no longer crash on leave

This commit is contained in:
stryan 2021-07-29 17:40:11 -04:00
parent 816e5cf45d
commit 884766dc72
4 changed files with 25 additions and 13 deletions

View File

@ -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,6 +168,10 @@ func backend(container *UIContainer) {
}
return
case <-ticker.C:
select {
case <-done:
return
default:
if matchID != uuid.Nil {
err := conn.WriteJSON(coordinator.SessionCommand{
ID: id,
@ -179,6 +185,7 @@ func backend(container *UIContainer) {
}
}
}
}
}
func GetCommand(uid uuid.UUID, resp chan coordinator.SessionCommand, container *UIContainer) {

View File

@ -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
}
}

View File

@ -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

View File

@ -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