From 0ca90b50a5cfe0a7d0c103aad0afba7b2402f5f3 Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Wed, 30 Oct 2024 08:54:58 +0100 Subject: [PATCH] feat: refactor client handling and introduce websocket client type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - add new websocket.Client type - change Connect and Disconnect functions to use the new Client type - implement methods for sending commands and updating settings on Client - update keyhandler to use websocket.Client instead of ChronoTomato.Client - modify UpdateLoop to accept websocket.Client - refactor ProcessServerMessages to accept the new Client type 🤖 --- internal/frontend/keyhandler.go | 8 ++++---- internal/frontend/main.go | 2 +- internal/websocket/connect.go | 8 +++----- internal/websocket/receive.go | 3 +-- internal/websocket/send.go | 14 ++++++++------ pkg/models/client.go | 8 ++++++++ 6 files changed, 25 insertions(+), 18 deletions(-) create mode 100644 pkg/models/client.go diff --git a/internal/frontend/keyhandler.go b/internal/frontend/keyhandler.go index 213a957..bd0805b 100644 --- a/internal/frontend/keyhandler.go +++ b/internal/frontend/keyhandler.go @@ -20,16 +20,16 @@ func start_pause_resume(message GoTomato.ServerMessage) string { } } -func keyhandler(key keyboard.KeyEvent, client ChronoTomato.Client, config ChronoTomato.Config, message GoTomato.ServerMessage) bool { +func keyhandler(key keyboard.KeyEvent, client websocket.Client, config ChronoTomato.Config, message GoTomato.ServerMessage) bool { switch key.Rune { case 0: // space cmd := start_pause_resume(message) - websocket.SendCmd(client, config.Password, cmd) + client.SendCmd(config.Password, cmd) case 115: // s - websocket.SendCmd(client, config.Password, "stop") + client.SendCmd(config.Password, "stop") case 114: // r if config.PomodoroConfig != (GoTomato.PomodoroConfig{}) { - websocket.Send_updateSettings(client, config.Password, config.PomodoroConfig) + client.SendSettingsUpdate(config.Password, config.PomodoroConfig) } case 113: // q return false diff --git a/internal/frontend/main.go b/internal/frontend/main.go index 0b51401..6a5888e 100644 --- a/internal/frontend/main.go +++ b/internal/frontend/main.go @@ -9,7 +9,7 @@ import ( GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) -func UpdateLoop(client ChronoTomato.Client, config ChronoTomato.Config, channel <-chan GoTomato.ServerMessage) { +func UpdateLoop(client websocket.Client, config ChronoTomato.Config, channel <-chan GoTomato.ServerMessage) { var message GoTomato.ServerMessage keysEvents, _ := keyboard.GetKeys(1) diff --git a/internal/websocket/connect.go b/internal/websocket/connect.go index 7a98d28..26d84a6 100644 --- a/internal/websocket/connect.go +++ b/internal/websocket/connect.go @@ -4,11 +4,9 @@ import ( "github.com/charmbracelet/log" "github.com/gorilla/websocket" "time" - - "git.smsvc.net/pomodoro/ChronoTomato/pkg/models" ) -func Connect(url string) models.Client { +func Connect(url string) Client { log.Info("Connected 󰖟 ", "host", url) conn, _, err := websocket.DefaultDialer.Dial(url, nil) @@ -16,10 +14,10 @@ func Connect(url string) models.Client { log.Fatal("Dial error!", "reason", err) } - return models.Client{Conn: conn} + return Client{Conn: conn} } -func Disconnect(client models.Client) { +func Disconnect(client Client) { select { case <-Done: // session closed by remote diff --git a/internal/websocket/receive.go b/internal/websocket/receive.go index 8c1b07a..547607b 100644 --- a/internal/websocket/receive.go +++ b/internal/websocket/receive.go @@ -6,13 +6,12 @@ import ( "github.com/charmbracelet/log" "github.com/gorilla/websocket" - ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models" GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) var Done = make(chan struct{}) -func ProcessServerMessages(client ChronoTomato.Client, channel chan<- GoTomato.ServerMessage) { +func ProcessServerMessages(client Client, channel chan<- GoTomato.ServerMessage) { var serverMessage GoTomato.ServerMessage defer close(Done) diff --git a/internal/websocket/send.go b/internal/websocket/send.go index c0cc6ba..fa736dd 100644 --- a/internal/websocket/send.go +++ b/internal/websocket/send.go @@ -9,35 +9,37 @@ import ( GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" ) -func sendClientCommand(client ChronoTomato.Client, msg GoTomato.ClientCommand) { +type Client ChronoTomato.PomodoroClient // New websocket client + +func (c Client) sendClientCommand(msg GoTomato.ClientCommand) { messageBytes, err := json.Marshal(msg) if err != nil { log.Error("Error marshalling!", "reason", err) return } - err = client.Conn.WriteMessage(websocket.TextMessage, messageBytes) + err = c.Conn.WriteMessage(websocket.TextMessage, messageBytes) if err != nil { log.Error("Write error!", "reason", err) return } } -func SendCmd(client ChronoTomato.Client, pwd string, cmd string) { +func (c Client) SendCmd(pwd string, cmd string) { message := GoTomato.ClientCommand{ Command: cmd, Password: pwd, } - sendClientCommand(client, message) + c.sendClientCommand(message) } -func Send_updateSettings(client ChronoTomato.Client, pwd string, settings GoTomato.PomodoroConfig) { +func (c Client) SendSettingsUpdate(pwd string, settings GoTomato.PomodoroConfig) { message := GoTomato.ClientCommand{ Command: "updateSettings", Password: pwd, Settings: settings, } - sendClientCommand(client, message) + c.sendClientCommand(message) } diff --git a/pkg/models/client.go b/pkg/models/client.go new file mode 100644 index 0000000..3cd5007 --- /dev/null +++ b/pkg/models/client.go @@ -0,0 +1,8 @@ +package models + +import "github.com/gorilla/websocket" + +// Represents a websocket client +type PomodoroClient struct { + Conn *websocket.Conn // The websocket connection of the client +}