fix: prevent concurrent write to websocket connection

- refactor client handling and message broadcasting
- replace Client struct
- implement SendMessage method in Client struct for safer message sending
- update client map to use *models.Client instead of bool
- adjust BroadcastMessage and RunPomodoroTimer functions to use new client type

🤖
This commit is contained in:
Sebastian Mark 2024-10-20 11:06:37 +02:00
parent ffc6913344
commit 4471c86a0c
7 changed files with 53 additions and 17 deletions

View file

@ -24,7 +24,7 @@ var pomodoroResumeChannel = make(chan bool, 1)
var mu sync.Mutex // to synchronize access to shared state
// RunPomodoroTimer iterates the Pomodoro work/break sessions.
func RunPomodoroTimer(clients map[*websocket.Conn]bool) {
func RunPomodoroTimer(clients map[*websocket.Conn]*models.Client) {
mu.Lock()
pomodoroRunning = true
pomodoroPaused = false
@ -51,7 +51,7 @@ func RunPomodoroTimer(clients map[*websocket.Conn]bool) {
}
// ResetPomodoro resets the running Pomodoro timer.
func ResetPomodoro(clients map[*websocket.Conn]bool) {
func ResetPomodoro(clients map[*websocket.Conn]*models.Client) {
mu.Lock()
pomodoroRunning = false // Reset the running state
pomodoroPaused = false // Reset the paused state