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-19 09:47:56 +00:00
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"log"
|
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-21 06:16:26 +00:00
|
|
|
log.Printf("Error marshalling message: %v", err)
|
|
|
|
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 {
|
|
|
|
log.Printf("Error broadcasting to client: %v", err)
|
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|