mirror of
https://github.com/fankes/komari-agent.git
synced 2025-10-19 11:09:22 +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
|
agent.json
|
||||||
.vscode/
|
.vscode/
|
||||||
komari-agent.exe
|
komari-agent.exe
|
||||||
|
@@ -33,11 +33,15 @@ func EstablishWebSocketConnection() {
|
|||||||
interval = flags.Interval - 1
|
interval = flags.Interval - 1
|
||||||
}
|
}
|
||||||
|
|
||||||
ticker := time.NewTicker(time.Duration(interval * float64(time.Second)))
|
dataTicker := time.NewTicker(time.Duration(interval * float64(time.Second)))
|
||||||
defer ticker.Stop()
|
defer dataTicker.Stop()
|
||||||
|
|
||||||
for range ticker.C {
|
heartbeatTicker := time.NewTicker(30 * time.Second)
|
||||||
// If no connection, attempt to connect
|
defer heartbeatTicker.Stop()
|
||||||
|
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-dataTicker.C:
|
||||||
if conn == nil {
|
if conn == nil {
|
||||||
log.Println("Attempting to connect to WebSocket...")
|
log.Println("Attempting to connect to WebSocket...")
|
||||||
retry := 0
|
retry := 0
|
||||||
@@ -71,6 +75,16 @@ func EstablishWebSocketConnection() {
|
|||||||
conn = nil // Mark connection as dead
|
conn = nil // Mark connection as dead
|
||||||
continue
|
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