add timeout consts in new file

This commit is contained in:
Sebastian Mark 2024-11-21 08:44:50 +01:00
parent 51b08b66e0
commit c76ea3628b
4 changed files with 9 additions and 5 deletions

View file

@ -12,6 +12,7 @@ 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) tick := time.NewTicker(time.Second)
for { for {
// Marshal the message into JSON format // Marshal the message into JSON format
jsonMessage, err := json.Marshal(shared.State) jsonMessage, err := json.Marshal(shared.State)

View file

@ -8,7 +8,7 @@ import (
) )
func sendPing(client *models.WebsocketClient) bool { func sendPing(client *models.WebsocketClient) bool {
client.Conn.SetWriteDeadline(time.Now().Add(10 * time.Second)) client.Conn.SetWriteDeadline(time.Now().Add(SEND_TIMEOUT * time.Second))
err := client.Conn.WriteMessage(websocket.PingMessage, nil) err := client.Conn.WriteMessage(websocket.PingMessage, nil)
if err != nil { if err != nil {
@ -19,12 +19,12 @@ func sendPing(client *models.WebsocketClient) bool {
} }
func isStale(client *models.WebsocketClient) bool { func isStale(client *models.WebsocketClient) bool {
return time.Since(client.LastPong) > (90 * time.Second) return time.Since(client.LastPong) > (STALE_CLIENT_TIMEOUT * time.Second)
} }
// Check and remove stale clients // Check and remove stale clients
func RemoveStaleClients() { func RemoveStaleClients() {
ticker := time.NewTicker(30 * time.Second) ticker := time.NewTicker(STALE_CHECK_INTERVALL * time.Second)
defer ticker.Stop() defer ticker.Stop()
for range ticker.C { for range ticker.C {

View file

@ -0,0 +1,5 @@
package websocket
const SEND_TIMEOUT = 10
const STALE_CLIENT_TIMEOUT = 90
const STALE_CHECK_INTERVALL = 30

View file

@ -6,8 +6,6 @@ import (
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
) )
const TIMEOUT = 10
// Represents a command from the client (start/stop) // Represents a command from the client (start/stop)
type ClientCommand struct { type ClientCommand struct {
Command string `json:"command"` // Command send to the server Command string `json:"command"` // Command send to the server