Sebastian Mark
ab2e8c161d
- add goroutine to remove stale clients from the connection pool
- update client struct to track `LastPong` time
- set write deadline for websocket connections
- move package variables and const to `vars.go`
- log additional information when broadcasting errors
🤖
35 lines
884 B
Go
35 lines
884 B
Go
package websocket
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/gorilla/websocket"
|
|
"time"
|
|
|
|
"git.smsvc.net/pomodoro/GoTomato/internal/helper"
|
|
"git.smsvc.net/pomodoro/GoTomato/internal/shared"
|
|
)
|
|
|
|
// Sends continous messages to all connected WebSocket clients
|
|
func SendPermanentBroadCastMessage() {
|
|
tick := time.NewTicker(time.Second)
|
|
|
|
for {
|
|
// Marshal the message into JSON format
|
|
jsonMessage, err := json.Marshal(shared.State)
|
|
if err != nil {
|
|
helper.Logger.Error("Error marshalling message:", "msg", err)
|
|
return
|
|
}
|
|
|
|
// Iterate over all connected clients and broadcast the message
|
|
for _, client := range Clients {
|
|
// Send message to client
|
|
err := client.SendMessage(websocket.TextMessage, jsonMessage)
|
|
if err != nil {
|
|
helper.Logger.Error("Error broadcasting to client:", "msg", err, "host", client.RealIP, "clients", len(Clients))
|
|
}
|
|
}
|
|
|
|
<-tick.C
|
|
}
|
|
}
|