feat: reconnect on disconnect (status in 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:
parent
1dd42d3c14
commit
9c4d43a4d3
6 changed files with 54 additions and 39 deletions
|
@ -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
|
||||
}
|
||||
|
|
|
@ -2,9 +2,10 @@ package websocket
|
|||
|
||||
import (
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
|
||||
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
|
||||
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
||||
)
|
||||
|
||||
|
@ -12,30 +13,35 @@ var Done = make(chan struct{})
|
|||
|
||||
// Receives websocket messages and writes them to a channel.
|
||||
// Closes the channel if websocket closes.
|
||||
func (c Client) ProcessServerMessages(channel chan<- GoTomato.ServerMessage) {
|
||||
var serverMessage GoTomato.ServerMessage
|
||||
func (c *Client) ProcessServerMessages(channel chan<- GoTomato.ServerMessage) {
|
||||
var serverMessage, prevMessage GoTomato.ServerMessage
|
||||
|
||||
defer close(Done)
|
||||
|
||||
for {
|
||||
_, message, err := c.Conn.ReadMessage()
|
||||
if err != nil {
|
||||
// On normal closure exit gracefully
|
||||
if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
|
||||
// Ignore normal closure and exit gracefully
|
||||
return
|
||||
}
|
||||
// Log any other errors
|
||||
helper.Logger.Error("Read error!", "reason", err)
|
||||
close(channel)
|
||||
return
|
||||
}
|
||||
|
||||
err = json.Unmarshal(message, &serverMessage)
|
||||
if err != nil {
|
||||
helper.Logger.Error("Error unmarshalling!", "reason", err)
|
||||
// Try to reconnect on unexpected disconnect
|
||||
for {
|
||||
channel <- prevMessage // send previous ServerMessage to update view
|
||||
time.Sleep(time.Second)
|
||||
*c = Connect(c.Server)
|
||||
if c.Connected() {
|
||||
break
|
||||
}
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
err = json.Unmarshal(message, &serverMessage)
|
||||
c.LastErr = err
|
||||
|
||||
channel <- serverMessage
|
||||
prevMessage = serverMessage
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue