2024-10-21 07:24:20 +00:00
|
|
|
package websocket
|
2024-10-19 09:47:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2024-10-21 07:20:58 +00:00
|
|
|
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
|
2024-10-25 22:01:07 +00:00
|
|
|
"github.com/charmbracelet/log"
|
2024-10-19 09:47:56 +00:00
|
|
|
"github.com/gorilla/websocket"
|
2024-10-21 06:16:26 +00:00
|
|
|
"time"
|
2024-10-19 09:47:56 +00:00
|
|
|
)
|
|
|
|
|
2024-10-21 07:37:07 +00:00
|
|
|
// sends continous messages to all connected WebSocket clients.
|
2024-10-21 06:16:26 +00:00
|
|
|
func SendPermanentBroadCastMessage() {
|
2024-10-25 21:15:48 +00:00
|
|
|
tick := time.NewTicker(time.Second)
|
2024-10-21 06:16:26 +00:00
|
|
|
for {
|
|
|
|
// Marshal the message into JSON format
|
2024-10-21 07:20:58 +00:00
|
|
|
jsonMessage, err := json.Marshal(shared.Message)
|
2024-10-19 09:47:56 +00:00
|
|
|
if err != nil {
|
2024-10-25 22:01:07 +00:00
|
|
|
log.Error("Error marshalling message:", "msg", err)
|
2024-10-21 06:16:26 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Iterate over all connected clients and broadcast the message
|
2024-10-21 07:24:20 +00:00
|
|
|
for _, client := range Clients {
|
2024-10-21 06:16:26 +00:00
|
|
|
err := client.SendMessage(websocket.TextMessage, jsonMessage)
|
|
|
|
if err != nil {
|
2024-10-25 22:01:07 +00:00
|
|
|
log.Error("Error broadcasting to client:", "msg", err)
|
2024-10-21 06:16:26 +00:00
|
|
|
// The client is responsible for closing itself on error
|
|
|
|
}
|
2024-10-19 09:47:56 +00:00
|
|
|
}
|
2024-10-25 21:15:48 +00:00
|
|
|
<-tick.C
|
2024-10-19 09:47:56 +00:00
|
|
|
}
|
|
|
|
}
|