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,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)
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package pomodoro
|
||||
|
||||
import (
|
||||
"git.smsvc.net/pomodoro/GoTomato/internal/broadcast"
|
||||
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
|
||||
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
"sync"
|
||||
)
|
||||
|
@ -15,14 +15,14 @@ var mu sync.Mutex // to synchronize access to shared state
|
|||
// RunPomodoro iterates the Pomodoro work/break sessions.
|
||||
func RunPomodoro(config models.GoTomatoPomodoroConfig) {
|
||||
mu.Lock()
|
||||
broadcast.Message.Ongoing = true
|
||||
broadcast.Message.Paused = false
|
||||
shared.Message.Ongoing = true
|
||||
shared.Message.Paused = false
|
||||
mu.Unlock()
|
||||
|
||||
broadcast.Message.TotalSession = config.Sessions
|
||||
shared.Message.TotalSession = config.Sessions
|
||||
|
||||
for session := 1; session <= config.Sessions; session++ {
|
||||
broadcast.Message.Session = session
|
||||
shared.Message.Session = session
|
||||
if !startTimer(config.Work, "Work") {
|
||||
break
|
||||
}
|
||||
|
@ -38,7 +38,7 @@ func RunPomodoro(config models.GoTomatoPomodoroConfig) {
|
|||
}
|
||||
|
||||
mu.Lock()
|
||||
broadcast.Message.Ongoing = false
|
||||
shared.Message.Ongoing = false
|
||||
mu.Unlock()
|
||||
}
|
||||
|
||||
|
@ -48,20 +48,20 @@ func ResetPomodoro() {
|
|||
pomodoroResetChannel <- true
|
||||
|
||||
mu.Lock()
|
||||
broadcast.Message.Ongoing = false
|
||||
broadcast.Message.Paused = false
|
||||
shared.Message.Ongoing = false
|
||||
shared.Message.Paused = false
|
||||
mu.Unlock()
|
||||
|
||||
// Reset message
|
||||
broadcast.Message.Mode = "none"
|
||||
broadcast.Message.Session = 0
|
||||
broadcast.Message.TotalSession = 0
|
||||
broadcast.Message.TimeLeft = 0
|
||||
shared.Message.Mode = "none"
|
||||
shared.Message.Session = 0
|
||||
shared.Message.TotalSession = 0
|
||||
shared.Message.TimeLeft = 0
|
||||
}
|
||||
|
||||
func PausePomodoro() {
|
||||
mu.Lock()
|
||||
broadcast.Message.Paused = true
|
||||
shared.Message.Paused = true
|
||||
mu.Unlock()
|
||||
|
||||
pomodoroPauseChannel <- true
|
||||
|
@ -69,7 +69,7 @@ func PausePomodoro() {
|
|||
|
||||
func ResumePomodoro() {
|
||||
mu.Lock()
|
||||
broadcast.Message.Paused = false
|
||||
shared.Message.Paused = false
|
||||
mu.Unlock()
|
||||
pomodoroResumeChannel <- true
|
||||
}
|
||||
|
@ -77,11 +77,11 @@ func ResumePomodoro() {
|
|||
func IsPomodoroOngoing() bool {
|
||||
mu.Lock()
|
||||
defer mu.Unlock() // Ensures that the mutex is unlocked after the function is done
|
||||
return broadcast.Message.Ongoing
|
||||
return shared.Message.Ongoing
|
||||
}
|
||||
|
||||
func IsPomodoroPaused() bool {
|
||||
mu.Lock()
|
||||
defer mu.Unlock() // Ensures that the mutex is unlocked after the function is done
|
||||
return broadcast.Message.Paused
|
||||
return shared.Message.Paused
|
||||
}
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
package pomodoro
|
||||
|
||||
import (
|
||||
"git.smsvc.net/pomodoro/GoTomato/internal/broadcast"
|
||||
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
|
||||
"time"
|
||||
)
|
||||
|
||||
// startTimer runs the countdown and broadcasts every second.
|
||||
// startTimer runs the countdown and shared. every second.
|
||||
func startTimer(remainingSeconds int, mode string) bool {
|
||||
for remainingSeconds > 0 {
|
||||
select {
|
||||
|
@ -18,16 +18,16 @@ func startTimer(remainingSeconds int, mode string) bool {
|
|||
default:
|
||||
// Broadcast the current state to all clients
|
||||
if !IsPomodoroPaused() {
|
||||
broadcast.Message.Mode = mode
|
||||
broadcast.Message.TimeLeft = remainingSeconds
|
||||
shared.Message.Mode = mode
|
||||
shared.Message.TimeLeft = remainingSeconds
|
||||
time.Sleep(time.Second)
|
||||
remainingSeconds--
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Final broadcast when time reaches zero
|
||||
broadcast.Message.TimeLeft = remainingSeconds
|
||||
// Final shared.when time reaches zero
|
||||
shared.Message.TimeLeft = remainingSeconds
|
||||
|
||||
return true
|
||||
}
|
||||
|
|
14
internal/shared/state.go
Normal file
14
internal/shared/state.go
Normal file
|
@ -0,0 +1,14 @@
|
|||
package shared
|
||||
|
||||
import (
|
||||
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
)
|
||||
|
||||
var Message = models.ServerMessage{
|
||||
Mode: "none",
|
||||
Session: 0,
|
||||
TotalSession: 0,
|
||||
TimeLeft: 0,
|
||||
Ongoing: false,
|
||||
Paused: false,
|
||||
}
|
|
@ -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…
Reference in a new issue