From 2266b94b7c69125d631297bb07831eba58d805fe Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Wed, 20 Nov 2024 09:32:04 +0100 Subject: [PATCH] refactor: update reconnect code for better understanding --- internal/websocket/connect.go | 3 ++- internal/websocket/receive.go | 19 +++++++------------ 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/internal/websocket/connect.go b/internal/websocket/connect.go index 1f35b7b..5e063c8 100644 --- a/internal/websocket/connect.go +++ b/internal/websocket/connect.go @@ -1,9 +1,10 @@ package websocket import ( - "github.com/gorilla/websocket" "time" + "github.com/gorilla/websocket" + ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models" ) diff --git a/internal/websocket/receive.go b/internal/websocket/receive.go index 510ad34..6aca107 100644 --- a/internal/websocket/receive.go +++ b/internal/websocket/receive.go @@ -22,27 +22,22 @@ func (c *Client) ProcessServerMessages(channel chan<- GoTomato.ServerMessage) { c.Conn.SetReadDeadline(time.Now().Add(10 * time.Second)) _, message, err := c.Conn.ReadMessage() if err != nil { + c.LastErr = err + // On normal closure exit gracefully if websocket.IsCloseError(err, websocket.CloseNormalClosure) { return } - c.LastErr = err - // Try to reconnect on unexpected disconnect - for { + // reset connection and reconnect + c.Conn = nil + pw := c.Password + for !c.Connected() { channel <- serverMessage // send last known ServerMessage to update view - time.Sleep(time.Second) - - // reconnect while preserving password - pw := c.Password *c = Connect(c.Server) - c.Password = pw - - if c.Connected() { - break - } } + c.Password = pw continue }