Sebastian Mark
4471c86a0c
- refactor client handling and message broadcasting
- replace Client struct
- implement SendMessage method in Client struct for safer message sending
- update client map to use *models.Client instead of bool
- adjust BroadcastMessage and RunPomodoroTimer functions to use new client type
🤖
27 lines
776 B
Go
27 lines
776 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.BroadcastMessage) {
|
|
// 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
|
|
}
|
|
}
|
|
}
|