Compare commits
3 commits
3960216647
...
16beea02ca
Author | SHA1 | Date | |
---|---|---|---|
16beea02ca | |||
f13bdff03f | |||
af5b578cb3 |
3 changed files with 6 additions and 11 deletions
|
@ -11,9 +11,10 @@ import (
|
|||
|
||||
// Sends continous messages to all connected WebSocket clients
|
||||
func SendPermanentBroadCastMessage() {
|
||||
tick := time.NewTicker(time.Second)
|
||||
ticker := time.NewTicker(BROADCAST_INTERVAL * time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
for {
|
||||
for range ticker.C {
|
||||
// Marshal the message into JSON format
|
||||
jsonMessage, err := json.Marshal(shared.State)
|
||||
if err != nil {
|
||||
|
@ -25,13 +26,12 @@ func SendPermanentBroadCastMessage() {
|
|||
mu.Lock()
|
||||
for _, client := range Clients {
|
||||
// Send message to client
|
||||
err := client.SendMessage(websocket.TextMessage, jsonMessage)
|
||||
client.Conn.SetWriteDeadline(time.Now().Add(SEND_TIMEOUT * time.Second))
|
||||
err := client.Conn.WriteMessage(websocket.TextMessage, jsonMessage)
|
||||
if err != nil {
|
||||
helper.Logger.Error("Error broadcasting to client:", "msg", err, "host", client.RealIP, "clients", len(Clients))
|
||||
}
|
||||
}
|
||||
mu.Unlock()
|
||||
|
||||
<-tick.C
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@ import (
|
|||
"git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
)
|
||||
|
||||
const BROADCAST_INTERVAL = 1
|
||||
const SEND_TIMEOUT = 10
|
||||
const STALE_CLIENT_TIMEOUT = 90
|
||||
const STALE_CHECK_INTERVALL = 30
|
||||
|
|
|
@ -19,9 +19,3 @@ type WebsocketClient struct {
|
|||
LastPong time.Time
|
||||
RealIP string
|
||||
}
|
||||
|
||||
// Sends a message to the websocket.
|
||||
func (c *WebsocketClient) SendMessage(messageType int, data []byte) error {
|
||||
c.Conn.SetWriteDeadline(time.Now().Add(TIMEOUT * time.Second))
|
||||
return c.Conn.WriteMessage(messageType, data)
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue