2024-10-19 09:47:56 +00:00
|
|
|
package broadcast
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"log"
|
2024-10-21 06:16:26 +00:00
|
|
|
"time"
|
2024-10-19 09:47:56 +00:00
|
|
|
)
|
|
|
|
|
2024-10-21 06:16:26 +00:00
|
|
|
// 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,
|
|
|
|
}
|
2024-10-19 09:47:56 +00:00
|
|
|
|
2024-10-21 06:16:26 +00:00
|
|
|
// BroadcastMessage sends a message to all connected WebSocket clients.
|
|
|
|
func SendPermanentBroadCastMessage() {
|
|
|
|
for {
|
|
|
|
// Marshal the message into JSON format
|
|
|
|
jsonMessage, err := json.Marshal(Message)
|
2024-10-19 09:47:56 +00:00
|
|
|
if err != nil {
|
2024-10-21 06:16:26 +00:00
|
|
|
log.Printf("Error marshalling message: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Iterate over all connected clients and broadcast the message
|
|
|
|
for _, client := range Clients {
|
|
|
|
err := client.SendMessage(websocket.TextMessage, jsonMessage)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("Error broadcasting to client: %v", err)
|
|
|
|
// The client is responsible for closing itself on error
|
|
|
|
}
|
2024-10-19 09:47:56 +00:00
|
|
|
}
|
2024-10-21 06:16:26 +00:00
|
|
|
time.Sleep(time.Second)
|
2024-10-19 09:47:56 +00:00
|
|
|
}
|
|
|
|
}
|