27 lines
773 B
Go
27 lines
773 B
Go
package broadcast
|
|
|
|
import (
|
|
"encoding/json"
|
|
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
|
"github.com/gorilla/websocket"
|
|
"log"
|
|
)
|
|
|
|
// 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
|
|
}
|
|
|
|
// 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
|
|
}
|
|
}
|
|
}
|