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)
|
err := connection.ReadJSON(&resp)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("Error in receive:", err)
|
log.Println("Error in receive:", err)
|
||||||
|
break
|
||||||
}
|
}
|
||||||
switch resp.Result {
|
switch resp.Result {
|
||||||
case coordinator.SessionRespJoined1:
|
case coordinator.SessionRespJoined1:
|
||||||
@ -55,7 +56,8 @@ func receiveHandler(connection *websocket.Conn, container *UIContainer) {
|
|||||||
}
|
}
|
||||||
case coordinator.SessionRespLeft:
|
case coordinator.SessionRespLeft:
|
||||||
container.Output <- "game left"
|
container.Output <- "game left"
|
||||||
break
|
matchID = uuid.Nil
|
||||||
|
return
|
||||||
case coordinator.SessionRespBroadcastSenTurn:
|
case coordinator.SessionRespBroadcastSenTurn:
|
||||||
container.Output <- "Sentinal may take their turn"
|
container.Output <- "Sentinal may take their turn"
|
||||||
case coordinator.SessionRespBroadcastScoTrun:
|
case coordinator.SessionRespBroadcastScoTrun:
|
||||||
@ -166,15 +168,20 @@ func backend(container *UIContainer) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
if matchID != uuid.Nil {
|
select {
|
||||||
err := conn.WriteJSON(coordinator.SessionCommand{
|
case <-done:
|
||||||
ID: id,
|
return
|
||||||
MatchID: matchID,
|
default:
|
||||||
Command: coordinator.SessionCmdPoll,
|
if matchID != uuid.Nil {
|
||||||
})
|
err := conn.WriteJSON(coordinator.SessionCommand{
|
||||||
if err != nil {
|
ID: id,
|
||||||
log.Println("Error writing to websocket:", err)
|
MatchID: matchID,
|
||||||
return
|
Command: coordinator.SessionCmdPoll,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
log.Println("Error writing to websocket:", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@ package main
|
|||||||
import (
|
import (
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"time"
|
||||||
|
|
||||||
"git.saintnet.tech/stryan/snengame/internal/coordinator"
|
"git.saintnet.tech/stryan/snengame/internal/coordinator"
|
||||||
"github.com/gorilla/websocket"
|
"github.com/gorilla/websocket"
|
||||||
@ -53,6 +54,7 @@ func serveWs(c *coordinator.Coordinator, w http.ResponseWriter, r *http.Request)
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
if resp.Result == coordinator.SessionRespLeft {
|
if resp.Result == coordinator.SessionRespLeft {
|
||||||
|
time.Sleep(1 * time.Second) //give clients a second to respond
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -122,6 +122,11 @@ func (c *Coordinator) Coordinate(cmd *SessionCommand) *SessionCommandResult {
|
|||||||
case SessionCmdPoll:
|
case SessionCmdPoll:
|
||||||
m, exists := c.Matches[cmd.MatchID]
|
m, exists := c.Matches[cmd.MatchID]
|
||||||
if exists {
|
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 {
|
select {
|
||||||
case res := <-m.Broadcasts[cmd.ID]:
|
case res := <-m.Broadcasts[cmd.ID]:
|
||||||
return &SessionCommandResult{
|
return &SessionCommandResult{
|
||||||
@ -158,7 +163,7 @@ func MatchWatcher(m *Session) {
|
|||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ticker.C:
|
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 {
|
if m.Game.Status == game.StatusLobby && !sen_ready && m.Game.SentinalPlayer.Ready {
|
||||||
for _, v := range m.Broadcasts {
|
for _, v := range m.Broadcasts {
|
||||||
v <- SessionRespBroadcastSenReady
|
v <- SessionRespBroadcastSenReady
|
||||||
|
@ -71,7 +71,6 @@ func (s *Session) Join(id uuid.UUID) SessionResp {
|
|||||||
for _, v := range s.Broadcasts {
|
for _, v := range s.Broadcasts {
|
||||||
v <- SessionRespBroadcastSenJoin
|
v <- SessionRespBroadcastSenJoin
|
||||||
}
|
}
|
||||||
s.Broadcasts[id] = make(chan SessionResp)
|
|
||||||
return SessionRespJoined1
|
return SessionRespJoined1
|
||||||
} else if s.p2 == uuid.Nil {
|
} else if s.p2 == uuid.Nil {
|
||||||
s.p2 = id
|
s.p2 = id
|
||||||
@ -79,7 +78,6 @@ func (s *Session) Join(id uuid.UUID) SessionResp {
|
|||||||
for _, v := range s.Broadcasts {
|
for _, v := range s.Broadcasts {
|
||||||
v <- SessionRespBroadcastScoJoin
|
v <- SessionRespBroadcastScoJoin
|
||||||
}
|
}
|
||||||
s.Broadcasts[id] = make(chan SessionResp)
|
|
||||||
return SessionRespJoined2
|
return SessionRespJoined2
|
||||||
} else {
|
} else {
|
||||||
return SessionRespJoinError
|
return SessionRespJoinError
|
||||||
|
Loading…
Reference in New Issue
Block a user