From b5d55ead4ad28dd654d587d58a1f0ffc7e933b46 Mon Sep 17 00:00:00 2001 From: henrygd Date: Sat, 12 Jul 2025 18:49:40 -0400 Subject: [PATCH] send websocket close message to agent --- beszel/internal/hub/agent_connect.go | 3 +-- beszel/internal/hub/systems/system.go | 2 +- beszel/internal/hub/ws/ws.go | 8 ++++---- 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/beszel/internal/hub/agent_connect.go b/beszel/internal/hub/agent_connect.go index b42978e..83a64aa 100644 --- a/beszel/internal/hub/agent_connect.go +++ b/beszel/internal/hub/agent_connect.go @@ -100,8 +100,7 @@ func (acr *agentConnectRequest) verifyWsConn(conn *gws.Conn, fpRecords []ws.Fing // make sure connection is closed if there is an error defer func() { if err != nil { - wsConn.Close() - acr.hub.Logger().Error("WebSocket error", "error", err, "systems", fpRecords) + wsConn.Close([]byte(err.Error())) } }() diff --git a/beszel/internal/hub/systems/system.go b/beszel/internal/hub/systems/system.go index 1fb30dc..0ee415c 100644 --- a/beszel/internal/hub/systems/system.go +++ b/beszel/internal/hub/systems/system.go @@ -365,7 +365,7 @@ func (sys *System) closeSSHConnection() { // The system will be set as down a few seconds later if the connection is not re-established. func (sys *System) closeWebSocketConnection() { if sys.WsConn != nil { - sys.WsConn.Close() + sys.WsConn.Close(nil) } } diff --git a/beszel/internal/hub/ws/ws.go b/beszel/internal/hub/ws/ws.go index cdb7216..e198739 100644 --- a/beszel/internal/hub/ws/ws.go +++ b/beszel/internal/hub/ws/ws.go @@ -77,7 +77,7 @@ func (h *Handler) OnMessage(conn *gws.Conn, message *gws.Message) { case wsConn.(*WsConn).responseChan <- message: default: // close if the connection is not expecting a response - wsConn.(*WsConn).Close() + wsConn.(*WsConn).Close(nil) } } @@ -100,9 +100,9 @@ func (h *Handler) OnClose(conn *gws.Conn, err error) { } // Close terminates the WebSocket connection gracefully. -func (ws *WsConn) Close() { +func (ws *WsConn) Close(msg []byte) { if ws.IsConnected() { - ws.conn.WriteClose(1000, nil) + ws.conn.WriteClose(1000, msg) } } @@ -130,7 +130,7 @@ func (ws *WsConn) RequestSystemData(data *system.CombinedData) error { }) select { case <-time.After(10 * time.Second): - ws.Close() + ws.Close(nil) return gws.ErrConnClosed case message = <-ws.responseChan: }