GoTomato/internal/websocket/broadcast.go

30 lines
775 B
Go

package websocket
import (
"encoding/json"
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
"github.com/gorilla/websocket"
"log"
"time"
)
// sends continous messages to all connected WebSocket clients.
func SendPermanentBroadCastMessage() {
for {
// Marshal the message into JSON format
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 {
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)
}
}