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:
parent
234f3c17dc
commit
aab6896c7d
6 changed files with 44 additions and 40 deletions
|
@ -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
|
||||
}
|
||||
|
||||
|
|
|
@ -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()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue