mirror of
https://github.com/fankes/komari-agent.git
synced 2025-10-18 18:49:23 +08:00
feat: https://github.com/komari-monitor/komari/issues/108 实现应用层的心跳机制
git忽略__debug
This commit is contained in:
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,3 +1,4 @@
|
||||
__debug*
|
||||
agent.json
|
||||
.vscode/
|
||||
komari-agent.exe
|
||||
|
@@ -33,11 +33,15 @@ func EstablishWebSocketConnection() {
|
||||
interval = flags.Interval - 1
|
||||
}
|
||||
|
||||
ticker := time.NewTicker(time.Duration(interval * float64(time.Second)))
|
||||
defer ticker.Stop()
|
||||
dataTicker := time.NewTicker(time.Duration(interval * float64(time.Second)))
|
||||
defer dataTicker.Stop()
|
||||
|
||||
for range ticker.C {
|
||||
// If no connection, attempt to connect
|
||||
heartbeatTicker := time.NewTicker(30 * time.Second)
|
||||
defer heartbeatTicker.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-dataTicker.C:
|
||||
if conn == nil {
|
||||
log.Println("Attempting to connect to WebSocket...")
|
||||
retry := 0
|
||||
@@ -71,6 +75,16 @@ func EstablishWebSocketConnection() {
|
||||
conn = nil // Mark connection as dead
|
||||
continue
|
||||
}
|
||||
case <-heartbeatTicker.C:
|
||||
if conn != nil {
|
||||
err := conn.WriteMessage(websocket.PingMessage, nil)
|
||||
if err != nil {
|
||||
log.Println("Failed to send heartbeat:", err)
|
||||
conn.Close()
|
||||
conn = nil // Mark connection as dead
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user