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,7 +2,6 @@ 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"
@ -25,7 +24,7 @@ func handleClientCommands(ws *websocket.Conn) {
_, message, err := ws.ReadMessage()
if err != nil {
log.Printf("Client disconnected: %v", err)
delete(broadcast.Clients, ws)
delete(Clients, ws)
break
}

View file

@ -1,7 +1,6 @@
package websocket
import (
"git.smsvc.net/pomodoro/GoTomato/internal/broadcast"
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
"github.com/gorilla/websocket"
"log"
@ -9,6 +8,8 @@ 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
@ -28,7 +29,7 @@ func HandleConnections(w http.ResponseWriter, r *http.Request) {
// Register the new client
mu.Lock()
broadcast.Clients[ws] = &models.Client{
Clients[ws] = &models.Client{
Conn: ws, // Store the WebSocket connection
}
mu.Unlock()