move stale client check to RemoveStaleClients()
This commit is contained in:
parent
ccd0c38b58
commit
7e5b0659b4
3 changed files with 22 additions and 5 deletions
|
@ -54,6 +54,7 @@ func Start() {
|
|||
r := http.NewServeMux()
|
||||
r.HandleFunc("/", websocket.HandleConnection)
|
||||
go websocket.SendPermanentBroadCastMessage()
|
||||
go websocket.RemoveStaleClients()
|
||||
|
||||
helper.Logger.Info("GoTomato started", "version", metadata.GoTomatoVersion)
|
||||
helper.Logger.Info("Websocket listening", "address", listen)
|
||||
|
|
|
@ -22,11 +22,6 @@ func SendPermanentBroadCastMessage() {
|
|||
|
||||
// Iterate over all connected clients and broadcast the message
|
||||
for _, client := range Clients {
|
||||
// Remove unresponsive client
|
||||
if client.IsStale() {
|
||||
client.Conn.Close()
|
||||
continue
|
||||
}
|
||||
// Send message to client
|
||||
err := client.SendMessage(websocket.TextMessage, jsonMessage)
|
||||
if err != nil {
|
||||
|
|
21
internal/websocket/staleClients.go
Normal file
21
internal/websocket/staleClients.go
Normal file
|
@ -0,0 +1,21 @@
|
|||
package websocket
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Check and remove stale clients
|
||||
func RemoveStaleClients() {
|
||||
ticker := time.NewTicker(30 * time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
for range ticker.C {
|
||||
mu.Lock()
|
||||
for _, client := range Clients {
|
||||
if client.IsStale() {
|
||||
client.Conn.Close()
|
||||
}
|
||||
}
|
||||
mu.Unlock()
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue