feat: improve broadcast message handling

- rename tick to ticker for clarity
- change infinite loop to range over ticker channel
- add defer statement to stop the ticker properly
- remove unnecessary channel receive operation

🤖
This commit is contained in:
Sebastian Mark 2024-11-21 18:49:10 +01:00
parent 2c3ccc86c3
commit efc34b57b2

View file

@ -11,9 +11,10 @@ import (
// Sends continous messages to all connected WebSocket clients // Sends continous messages to all connected WebSocket clients
func SendPermanentBroadCastMessage() { func SendPermanentBroadCastMessage() {
tick := time.NewTicker(time.Second) ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for { for range ticker.C {
// Marshal the message into JSON format // Marshal the message into JSON format
jsonMessage, err := json.Marshal(shared.State) jsonMessage, err := json.Marshal(shared.State)
if err != nil { if err != nil {
@ -32,7 +33,5 @@ func SendPermanentBroadCastMessage() {
} }
} }
mu.Unlock() mu.Unlock()
<-tick.C
} }
} }