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,27 +4,30 @@ import (
"encoding/json"
"github.com/gorilla/websocket"
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
// Sends a message to the clients websocket
func sendClientCommand(c Client, msg GoTomato.ClientCommand) {
func sendClientCommand(c *Client, msg GoTomato.ClientCommand) {
if !c.Connected() {
// noop if client is not connected
return
}
messageBytes, err := json.Marshal(msg)
if err != nil {
helper.Logger.Error("Error marshalling!", "reason", err)
c.LastErr = err
return
}
err = c.Conn.WriteMessage(websocket.TextMessage, messageBytes)
if err != nil {
helper.Logger.Error("Write error!", "reason", err)
c.LastErr = err
return
}
}
// Sends the passed command to the server
func (c Client) SendCmd(cmd string) {
func (c *Client) SendCmd(cmd string) {
message := GoTomato.ClientCommand{
Command: cmd,
Password: c.Password,
@ -34,7 +37,7 @@ func (c Client) SendCmd(cmd string) {
}
// Sends the new PomodoroConfig to the server
func (c Client) SendSettingsUpdate(settings GoTomato.PomodoroConfig) {
func (c *Client) SendSettingsUpdate(settings GoTomato.PomodoroConfig) {
message := GoTomato.ClientCommand{
Command: "updateSettings",
Password: c.Password,