From 6187122b81240d347e7b0b145264384e6a0a7147 Mon Sep 17 00:00:00 2001 From: Sebastian Mark Date: Wed, 30 Oct 2024 09:51:59 +0100 Subject: [PATCH] feat: refactor client methods MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - change `ProcessServerMessages` to use receiver instead of parameter - update `Disconnect` method to use receiver for better encapsulation - move `Client` definition to `connect.go` 🤖 --- cmd/client/main.go | 4 ++-- internal/websocket/connect.go | 8 ++++++-- internal/websocket/receive.go | 4 ++-- internal/websocket/send.go | 3 --- 4 files changed, 10 insertions(+), 9 deletions(-) diff --git a/cmd/client/main.go b/cmd/client/main.go index d1e7850..de0126c 100644 --- a/cmd/client/main.go +++ b/cmd/client/main.go @@ -49,8 +49,8 @@ func Start() { client.Password = config.Password channel := make(chan GoTomato.ServerMessage) - go websocket.ProcessServerMessages(client, channel) + go client.ProcessServerMessages(channel) frontend.UpdateLoop(client, config, channel) - websocket.Disconnect(client) + client.Disconnect() } diff --git a/internal/websocket/connect.go b/internal/websocket/connect.go index 26d84a6..4a2f2b8 100644 --- a/internal/websocket/connect.go +++ b/internal/websocket/connect.go @@ -4,8 +4,12 @@ import ( "github.com/charmbracelet/log" "github.com/gorilla/websocket" "time" + + ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models" ) +type Client ChronoTomato.GoTomatoClient // New websocket client + func Connect(url string) Client { log.Info("Connected 󰖟 ", "host", url) @@ -17,7 +21,7 @@ func Connect(url string) Client { return Client{Conn: conn} } -func Disconnect(client Client) { +func (c Client) Disconnect() { select { case <-Done: // session closed by remote @@ -25,7 +29,7 @@ func Disconnect(client Client) { default: // Cleanly close the connection by sending a close message and then // waiting (with timeout) for the server to close the connection. - client.Conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) + c.Conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, "")) select { case <-Done: case <-time.After(time.Second): diff --git a/internal/websocket/receive.go b/internal/websocket/receive.go index 547607b..c6ec7e8 100644 --- a/internal/websocket/receive.go +++ b/internal/websocket/receive.go @@ -11,13 +11,13 @@ import ( var Done = make(chan struct{}) -func ProcessServerMessages(client Client, channel chan<- GoTomato.ServerMessage) { +func (c Client) ProcessServerMessages(channel chan<- GoTomato.ServerMessage) { var serverMessage GoTomato.ServerMessage defer close(Done) for { - _, message, err := client.Conn.ReadMessage() + _, message, err := c.Conn.ReadMessage() if err != nil { if websocket.IsCloseError(err, websocket.CloseNormalClosure) { // Ignore normal closure and exit gracefully diff --git a/internal/websocket/send.go b/internal/websocket/send.go index 2d891ba..bc958d6 100644 --- a/internal/websocket/send.go +++ b/internal/websocket/send.go @@ -5,12 +5,9 @@ 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" ) -type Client ChronoTomato.GoTomatoClient // New websocket client - func (c Client) sendClientCommand(msg GoTomato.ClientCommand) { messageBytes, err := json.Marshal(msg) if err != nil {