diff --git a/.gitignore b/.gitignore index dd1c751..594b371 100644 --- a/.gitignore +++ b/.gitignore @@ -3,5 +3,6 @@ alpamon.log alpamon.db /alpamon +/cmd/alpamon/alpamon .DS_Store .idea \ No newline at end of file diff --git a/pkg/runner/client.go b/pkg/runner/client.go index 35cf288..c7a1d3f 100644 --- a/pkg/runner/client.go +++ b/pkg/runner/client.go @@ -70,8 +70,6 @@ func (wc *WebsocketClient) RunForever(ctx context.Context) { wc.CloseAndReconnect(ctx) continue } - // Sends "ping" query for Alpacon to verify WebSocket session status without error handling. - _ = wc.SendPingQuery() wc.CommandRequestHandler(message) } } @@ -87,6 +85,14 @@ func (wc *WebsocketClient) SendPingQuery() error { return nil } +func (wc *WebsocketClient) SendPongResponse() error { + pongResponse := map[string]string{ + "query": "pong", + "timestamp": time.Now().UTC().Format(time.RFC3339), + } + return wc.WriteJSON(pongResponse) +} + func (wc *WebsocketClient) ReadMessage() (messageType int, message []byte, err error) { messageType, message, err = wc.Conn.ReadMessage() if err != nil { @@ -221,6 +227,10 @@ func (wc *WebsocketClient) CommandRequestHandler(message []byte) { } switch content.Query { + case "ping": + if err := wc.SendPongResponse(); err != nil { + log.Debug().Err(err).Msg("Failed to send pong response.") + } case "command": scheduler.Rqueue.Post(fmt.Sprintf(eventCommandAckURL, content.Command.ID), nil, diff --git a/pkg/runner/command.go b/pkg/runner/command.go index c529530..ec0254f 100644 --- a/pkg/runner/command.go +++ b/pkg/runner/command.go @@ -134,6 +134,7 @@ func (cr *CommandRunner) handleInternalCmd() (int, string) { case "moduser": return cr.modUser() case "ping": + _ = cr.wsClient.SendPongResponse() return 0, time.Now().Format(time.RFC3339) case "download": return cr.runFileDownload(args[1])