feat: refactor client methods

- change `ProcessServerMessages` to use receiver instead of parameter
- update `Disconnect` method to use receiver for better encapsulation
- move `Client` definition to `connect.go`

🤖
This commit is contained in:
Sebastian Mark 2024-10-30 09:51:59 +01:00
parent 480f1c0cdd
commit 6187122b81
4 changed files with 10 additions and 9 deletions

View file

@ -49,8 +49,8 @@ func Start() {
client.Password = config.Password client.Password = config.Password
channel := make(chan GoTomato.ServerMessage) channel := make(chan GoTomato.ServerMessage)
go websocket.ProcessServerMessages(client, channel) go client.ProcessServerMessages(channel)
frontend.UpdateLoop(client, config, channel) frontend.UpdateLoop(client, config, channel)
websocket.Disconnect(client) client.Disconnect()
} }

View file

@ -4,8 +4,12 @@ import (
"github.com/charmbracelet/log" "github.com/charmbracelet/log"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"time" "time"
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
) )
type Client ChronoTomato.GoTomatoClient // New websocket client
func Connect(url string) Client { func Connect(url string) Client {
log.Info("Connected 󰖟 ", "host", url) log.Info("Connected 󰖟 ", "host", url)
@ -17,7 +21,7 @@ func Connect(url string) Client {
return Client{Conn: conn} return Client{Conn: conn}
} }
func Disconnect(client Client) { func (c Client) Disconnect() {
select { select {
case <-Done: case <-Done:
// session closed by remote // session closed by remote
@ -25,7 +29,7 @@ func Disconnect(client Client) {
default: default:
// Cleanly close the connection by sending a close message and then // Cleanly close the connection by sending a close message and then
// waiting (with timeout) for the server to close the connection. // 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 { select {
case <-Done: case <-Done:
case <-time.After(time.Second): case <-time.After(time.Second):

View file

@ -11,13 +11,13 @@ import (
var Done = make(chan struct{}) 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 var serverMessage GoTomato.ServerMessage
defer close(Done) defer close(Done)
for { for {
_, message, err := client.Conn.ReadMessage() _, message, err := c.Conn.ReadMessage()
if err != nil { if err != nil {
if websocket.IsCloseError(err, websocket.CloseNormalClosure) { if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
// Ignore normal closure and exit gracefully // Ignore normal closure and exit gracefully

View file

@ -5,12 +5,9 @@ import (
"github.com/charmbracelet/log" "github.com/charmbracelet/log"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models" GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
) )
type Client ChronoTomato.GoTomatoClient // New websocket client
func (c Client) sendClientCommand(msg GoTomato.ClientCommand) { func (c Client) sendClientCommand(msg GoTomato.ClientCommand) {
messageBytes, err := json.Marshal(msg) messageBytes, err := json.Marshal(msg)
if err != nil { if err != nil {