feat: implement permanent broadcast message functionality
- add SendPermanentBroadCastMessage to continuously send updates
- refactor BroadcastMessage to use a shared Message struct
- update pomodoro logic to modify broadcast.Message directly
- adjust client command handling to use broadcast.Clients map
- enhance ServerMessage struct with "Ongoing" and "Paused" fields
🤖
This commit is contained in:
parent
eba4065c6f
commit
9615d4d449
8 changed files with 75 additions and 65 deletions
|
@ -5,23 +5,37 @@ import (
|
|||
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
"github.com/gorilla/websocket"
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
// BroadcastMessage sends a message to all connected WebSocket clients.
|
||||
func BroadcastMessage(clients map[*websocket.Conn]*models.Client, message models.ServerMessage) {
|
||||
// Marshal the message into JSON format
|
||||
jsonMessage, err := json.Marshal(message)
|
||||
if err != nil {
|
||||
log.Printf("Error marshalling message: %v", err)
|
||||
return
|
||||
}
|
||||
// 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,
|
||||
}
|
||||
|
||||
// Iterate over all connected clients and broadcast the message
|
||||
for _, client := range clients {
|
||||
err := client.SendMessage(websocket.TextMessage, jsonMessage)
|
||||
// BroadcastMessage sends a message to all connected WebSocket clients.
|
||||
func SendPermanentBroadCastMessage() {
|
||||
for {
|
||||
// Marshal the message into JSON format
|
||||
jsonMessage, err := json.Marshal(Message)
|
||||
if err != nil {
|
||||
log.Printf("Error broadcasting to client: %v", err)
|
||||
// The client is responsible for closing itself on error
|
||||
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
|
||||
}
|
||||
}
|
||||
time.Sleep(time.Second)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue