feat: implement permanent broadcast message functionality

- add SendPermanentBroadCastMessage to continuously send updates
- refactor BroadcastMessage to use a shared Message struct
- update pomodoro logic to modify broadcast.Message directly
- adjust client command handling to use broadcast.Clients map
- enhance ServerMessage struct with "Ongoing" and "Paused" fields

🤖
This commit is contained in:
Sebastian Mark 2024-10-21 08:16:26 +02:00
parent eba4065c6f
commit 9615d4d449
8 changed files with 75 additions and 65 deletions

View file

@ -2,6 +2,7 @@ package websocket
import (
"encoding/json"
"git.smsvc.net/pomodoro/GoTomato/internal/broadcast"
"git.smsvc.net/pomodoro/GoTomato/internal/pomodoro"
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
"github.com/gorilla/websocket"
@ -24,7 +25,7 @@ func handleClientCommands(ws *websocket.Conn) {
_, message, err := ws.ReadMessage()
if err != nil {
log.Printf("Client disconnected: %v", err)
delete(Clients, ws)
delete(broadcast.Clients, ws)
break
}
@ -42,11 +43,11 @@ func handleClientCommands(ws *websocket.Conn) {
if clientCommand.Config != unsetPomodoroConfig {
pomodoroConfig = clientCommand.Config
}
go pomodoro.RunPomodoro(Clients, pomodoroConfig) // Start the timer with the list of clients
go pomodoro.RunPomodoro(broadcast.Clients, pomodoroConfig) // Start the timer with the list of clients
}
case "stop":
if pomodoro.IsPomodoroOngoing() {
pomodoro.ResetPomodoro(Clients) // Reset Pomodoro
pomodoro.ResetPomodoro(broadcast.Clients) // Reset Pomodoro
}
case "pause":
if pomodoro.IsPomodoroOngoing() && !pomodoro.IsPomodoroPaused() {

View file

@ -1,6 +1,7 @@
package websocket
import (
"git.smsvc.net/pomodoro/GoTomato/internal/broadcast"
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
"github.com/gorilla/websocket"
"log"
@ -8,8 +9,6 @@ import (
"sync"
)
// Clients is a map of connected WebSocket clients, where each client is represented by the Client struct
var Clients = make(map[*websocket.Conn]*models.Client)
var mu sync.Mutex // Mutex to protect access to the Clients map
// Upgrader to upgrade HTTP requests to WebSocket connections
@ -29,7 +28,7 @@ func HandleConnections(w http.ResponseWriter, r *http.Request) {
// Register the new client
mu.Lock()
Clients[ws] = &models.Client{
broadcast.Clients[ws] = &models.Client{
Conn: ws, // Store the WebSocket connection
}
mu.Unlock()