GoTomato/internal/websocket/broadcast.go
Sebastian Mark 600d2ed2ff feat: implement centralized logging helper
- remove direct log usage
- create a new logging helper
- update all log calls to use the new Logger instance
- set timestamp to ISO8601

🤖
2024-11-04 20:33:25 +01:00

32 lines
865 B
Go

package websocket
import (
"encoding/json"
"github.com/gorilla/websocket"
"time"
"git.smsvc.net/pomodoro/GoTomato/internal/helper"
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
)
// Sends continous messages to all connected WebSocket clients
func SendPermanentBroadCastMessage() {
tick := time.NewTicker(time.Second)
for {
// Marshal the message into JSON format
jsonMessage, err := json.Marshal(shared.State)
if err != nil {
helper.Logger.Error("Error marshalling message:", "msg", err)
return
}
// Iterate over all connected clients and broadcast the message
for _, client := range Clients {
err := client.SendMessage(websocket.TextMessage, jsonMessage)
if err != nil {
helper.Logger.Error("Error broadcasting to client:", "msg", err)
// The client is responsible for closing itself on error
}
}
<-tick.C
}
}