refactor: move broadcast.Message to shared.Message

- create a new shared state file to manage message state
- replace broadcast.Message with shared.Message in pomodoro logic
- update websocket client handling to use shared.Clients
- remove unnecessary broadcast imports from various files
- ensure consistent message handling across the application

🤖
This commit is contained in:
Sebastian Mark 2024-10-21 09:20:58 +02:00
parent 234f3c17dc
commit aab6896c7d
6 changed files with 44 additions and 40 deletions

View file

@ -2,34 +2,24 @@ package broadcast
import (
"encoding/json"
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
ws "git.smsvc.net/pomodoro/GoTomato/internal/websocket"
"github.com/gorilla/websocket"
"log"
"time"
)
// 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 Message = models.ServerMessage{
Mode: "none",
Session: 0,
TotalSession: 0,
TimeLeft: 0,
Ongoing: false,
Paused: false,
}
// BroadcastMessage sends a message to all connected WebSocket clients.
func SendPermanentBroadCastMessage() {
for {
// Marshal the message into JSON format
jsonMessage, err := json.Marshal(Message)
jsonMessage, err := json.Marshal(shared.Message)
if err != nil {
log.Printf("Error marshalling message: %v", err)
return
}
// Iterate over all connected clients and broadcast the message
for _, client := range Clients {
for _, client := range ws.Clients {
err := client.SendMessage(websocket.TextMessage, jsonMessage)
if err != nil {
log.Printf("Error broadcasting to client: %v", err)