Skip to content

Commit

Permalink
Fix closed websockets
Browse files Browse the repository at this point in the history
  • Loading branch information
windler committed Apr 22, 2022
1 parent 1bba676 commit 5ed1b56
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 10 deletions.
7 changes: 6 additions & 1 deletion cmd/chesspal/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,10 @@ func main() {
if !errors.Is(err, nil) {
log.Println(err)
}
defer ws.Close()
defer func() {
wsUI.RemoveWebsocket(ws)
ws.Close()
}()

if err := ws.WriteJSON(WSResponse{Bots: config.Bots, Humans: config.Humans}); !errors.Is(err, nil) {
log.Printf("error occurred: %v", err)
Expand Down Expand Up @@ -348,6 +351,8 @@ type Started struct {
}

func sendGameStarted(ws *websocket.Conn) {
ws.SetWriteDeadline(time.Now().Add(time.Second * 5))

if err := ws.WriteJSON(&Started{Started: true}); !errors.Is(err, nil) {
log.Printf("error occurred: %v", err)
}
Expand Down
77 changes: 77 additions & 0 deletions configs/chesspal.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,62 @@ bots:
options:
- UCI_LimitStrength=true
- UCI_Elo=800
- name: Dillan (fairy 900)
engine: fairy_stockfish
depth: 9
moveTimeMs: 300
threads: 1
options:
- UCI_LimitStrength=true
- UCI_Elo=900
- name: Etienne (fairy 1000)
engine: fairy_stockfish
depth: 9
moveTimeMs: 300
threads: 1
options:
- UCI_LimitStrength=true
- UCI_Elo=1000
- name: Furgy (fairy 1100)
engine: fairy_stockfish
depth: 9
moveTimeMs: 300
threads: 1
options:
- UCI_LimitStrength=true
- UCI_Elo=1100
- name: Gerald (fairy 1200)
engine: fairy_stockfish
depth: 9
moveTimeMs: 300
threads: 1
options:
- UCI_LimitStrength=true
- UCI_Elo=1200
- name: Herold (fairy 1300)
engine: fairy_stockfish
depth: 9
moveTimeMs: 300
threads: 1
options:
- UCI_LimitStrength=true
- UCI_Elo=1300
- name: Indira (fairy 1400)
engine: fairy_stockfish
depth: 9
moveTimeMs: 300
threads: 1
options:
- UCI_LimitStrength=true
- UCI_Elo=1400
- name: Jenne (fairy 1500)
engine: fairy_stockfish
depth: 9
moveTimeMs: 500
threads: 1
options:
- UCI_LimitStrength=true
- UCI_Elo=1500
- name: Lichess 1
engine: fairy_stockfish
depth: 5
Expand All @@ -57,6 +113,27 @@ bots:
threads: 1
options:
- Skill Level=-1
- name: Lichess 4
engine: fairy_stockfish
depth: 5
moveTimeMs: 200
threads: 1
options:
- Skill Level=3
- name: Lichess 5
engine: fairy_stockfish
depth: 5
moveTimeMs: 300
threads: 1
options:
- Skill Level=7
- name: Lichess 6
engine: fairy_stockfish
depth: 8
moveTimeMs: 400
threads: 1
options:
- Skill Level=11
eval:
engine: stockfish_12
depth: 20
Expand Down
28 changes: 19 additions & 9 deletions pkg/ui/webosockets.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"image/color"
"log"
"sync"
"time"

"github.com/gorilla/websocket"
"github.com/notnil/chess"
Expand All @@ -14,24 +15,29 @@ import (
)

type WSUI struct {
sockets []*websocket.Conn
sockets map[*websocket.Conn]*sync.Mutex
mutex *sync.Mutex
currentState *GameState
}

func NewWS() *WSUI {
game := chess.NewGame()
return &WSUI{
mutex: &sync.Mutex{},
mutex: &sync.Mutex{},
sockets: make(map[*websocket.Conn]*sync.Mutex),
currentState: &GameState{
SVGPosition: util.GetSVG(*game.Position().Board()),
},
}
}

func (u *WSUI) AddWebsocket(ws *websocket.Conn) {
u.sendCurentState(ws)
u.sockets = append(u.sockets, ws)
u.sockets[ws] = &sync.Mutex{}
u.sendCurentState(ws, u.sockets[ws])
}

func (u *WSUI) RemoveWebsocket(ws *websocket.Conn) {
delete(u.sockets, ws)
}

type GameState struct {
Expand Down Expand Up @@ -123,16 +129,20 @@ func (u *WSUI) Render(g chess.Game, action game.UIAction) {

u.currentState.Outcome = g.Outcome().String()

for _, ws := range u.sockets {
u.sendCurentState(ws)
for ws, mutex := range u.sockets {
go u.sendCurentState(ws, mutex)
}
u.mutex.Unlock()
}

func (u *WSUI) sendCurentState(ws *websocket.Conn) {
func (u *WSUI) sendCurentState(ws *websocket.Conn, mutex *sync.Mutex) {
mutex.Lock()
ws.SetWriteDeadline(time.Now().Add(time.Second * 5))

if err := ws.WriteJSON(u.currentState); !errors.Is(err, nil) {
log.Printf("error occurred: %v", err)
}
mutex.Unlock()
}

func (u *WSUI) Reset() {
Expand All @@ -143,7 +153,7 @@ func (u *WSUI) SendBoard(board chess.Board) {
u.currentState = &GameState{
SVGPosition: util.GetSVG(board),
}
for _, ws := range u.sockets {
u.sendCurentState(ws)
for ws, mutex := range u.sockets {
go u.sendCurentState(ws, mutex)
}
}

0 comments on commit 5ed1b56

Please sign in to comment.