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 }