opt into polling, clients no longer crash on leave
This commit is contained in:
parent
816e5cf45d
commit
884766dc72
@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user