feat: reconnect on disconnect (add status to TUI)

- add `Connected()` method for client
- add `LastErr` field to GoTomatoClient
  - replace error logging by updating `LastErr` field
  - modify methods to use pointer receivers for Client
- add `prevMessage` variable to store the last received server message
- show connect status in TUI
- use EnterAltScreen instead of ClearScreen
This commit is contained in:
Sebastian Mark 2024-11-06 09:03:25 +01:00
parent 1dd42d3c14
commit 05e8bf5854
6 changed files with 54 additions and 39 deletions

View file

@ -4,7 +4,6 @@ import (
"github.com/gorilla/websocket"
"time"
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
)
@ -13,30 +12,24 @@ type Client ChronoTomato.GoTomatoClient // New websocket client
// Connects to websocket
func Connect(url string) Client {
conn, _, err := websocket.DefaultDialer.Dial(url, nil)
if err != nil {
helper.Logger.Fatal("Dial error!", "reason", err)
}
helper.Logger.Info("Connected 󰖟 ", "host", url)
time.Sleep(time.Second)
return Client{Conn: conn, Server: url, LastErr: err}
}
return Client{Conn: conn}
func (c Client) Connected() bool {
return c.Conn != nil
}
// Disconnects from websocket
func (c Client) Disconnect() {
select {
case <-Done:
// session closed by remote
return
default:
// Cleanly close the connection by sending a close message and then
// waiting (with timeout) for the server to close the connection.
// Cleanly close the connection by sending a close message and then
// waiting (with timeout) for the server to close the connection.
if c.Connected() {
c.Conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
select {
case <-Done:
case <-time.After(time.Second):
}
return
}
return
}