feat: improve broadcast loop ticker

- 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 af5b578cb3
commit f13bdff03f

View file

@ -11,9 +11,10 @@ import (
// Sends continous messages to all connected WebSocket clients
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
jsonMessage, err := json.Marshal(shared.State)
if err != nil {
@ -32,7 +33,5 @@ func SendPermanentBroadCastMessage() {
}
}
mu.Unlock()
<-tick.C
}
}