feat: refactor connection handling to use models.Client

- introduce `Client` model
- modify all websocket operations to use the new `Client` model
- update function signatures to accept `models.Client` instead of `*websocket.Conn`
- ensure consistent usage of `client` across all related functions

🤖
This commit is contained in:
Sebastian Mark 2024-10-30 08:28:22 +01:00
parent f1071f33c6
commit b943c9d6eb
6 changed files with 26 additions and 24 deletions

View file

@ -35,11 +35,11 @@ func Start() {
config = helper.ParseConfig(*configfile)
}
conn := websocket.Connect(config.URL)
client := websocket.Connect(config.URL)
channel := make(chan GoTomato.ServerMessage)
go websocket.ProcessServerMessages(conn, channel)
frontend.UpdateLoop(conn, config, channel)
go websocket.ProcessServerMessages(client, channel)
frontend.UpdateLoop(client, config, channel)
websocket.Disconnect(conn)
websocket.Disconnect(client)
}

View file

@ -2,7 +2,6 @@ package frontend
import (
"github.com/eiannone/keyboard"
ws "github.com/gorilla/websocket"
"git.smsvc.net/pomodoro/ChronoTomato/internal/websocket"
@ -21,16 +20,16 @@ func start_pause_resume(message GoTomato.ServerMessage) string {
}
}
func keyhandler(key keyboard.KeyEvent, conn *ws.Conn, config ChronoTomato.Config, message GoTomato.ServerMessage) bool {
func keyhandler(key keyboard.KeyEvent, client ChronoTomato.Client, config ChronoTomato.Config, message GoTomato.ServerMessage) bool {
switch key.Rune {
case 0: // space
cmd := start_pause_resume(message)
websocket.SendCmd(conn, config.Password, cmd)
websocket.SendCmd(client, config.Password, cmd)
case 115: // s
websocket.SendCmd(conn, config.Password, "stop")
websocket.SendCmd(client, config.Password, "stop")
case 114: // r
if config.PomodoroConfig != (GoTomato.PomodoroConfig{}) {
websocket.Send_updateSettings(conn, config.Password, config.PomodoroConfig)
websocket.Send_updateSettings(client, config.Password, config.PomodoroConfig)
}
case 113: // q
return false

View file

@ -2,7 +2,6 @@ package frontend
import (
"github.com/eiannone/keyboard"
ws "github.com/gorilla/websocket"
"git.smsvc.net/pomodoro/ChronoTomato/internal/websocket"
@ -10,7 +9,7 @@ import (
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
func UpdateLoop(conn *ws.Conn, config ChronoTomato.Config, channel <-chan GoTomato.ServerMessage) {
func UpdateLoop(client ChronoTomato.Client, config ChronoTomato.Config, channel <-chan GoTomato.ServerMessage) {
var message GoTomato.ServerMessage
keysEvents, _ := keyboard.GetKeys(1)
@ -22,7 +21,7 @@ func UpdateLoop(conn *ws.Conn, config ChronoTomato.Config, channel <-chan GoToma
desktopNotifications(message)
terminalOutput(message)
case keypress := <-keysEvents:
if !keyhandler(keypress, conn, config, message) {
if !keyhandler(keypress, client, config, message) {
return
}
case <-websocket.Done:

View file

@ -4,9 +4,11 @@ import (
"github.com/charmbracelet/log"
"github.com/gorilla/websocket"
"time"
"git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
)
func Connect(url string) *websocket.Conn {
func Connect(url string) models.Client {
log.Info("Connected 󰖟 ", "host", url)
conn, _, err := websocket.DefaultDialer.Dial(url, nil)
@ -14,10 +16,10 @@ func Connect(url string) *websocket.Conn {
log.Fatal("Dial error!", "reason", err)
}
return conn
return models.Client{Conn: conn}
}
func Disconnect(conn *websocket.Conn) {
func Disconnect(client models.Client) {
select {
case <-Done:
// session closed by remote
@ -25,7 +27,7 @@ func Disconnect(conn *websocket.Conn) {
default:
// Cleanly close the connection by sending a close message and then
// waiting (with timeout) for the server to close the connection.
conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
client.Conn.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
select {
case <-Done:
case <-time.After(time.Second):

View file

@ -6,18 +6,19 @@ 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(conn *websocket.Conn, channel chan<- GoTomato.ServerMessage) {
func ProcessServerMessages(client ChronoTomato.Client, channel chan<- GoTomato.ServerMessage) {
var serverMessage GoTomato.ServerMessage
defer close(Done)
for {
_, message, err := conn.ReadMessage()
_, message, err := client.Conn.ReadMessage()
if err != nil {
if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
// Ignore normal closure and exit gracefully

View file

@ -5,38 +5,39 @@ 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"
)
func sendClientCommand(conn *websocket.Conn, msg GoTomato.ClientCommand) {
func sendClientCommand(client ChronoTomato.Client, msg GoTomato.ClientCommand) {
messageBytes, err := json.Marshal(msg)
if err != nil {
log.Error("Error marshalling!", "reason", err)
return
}
err = conn.WriteMessage(websocket.TextMessage, messageBytes)
err = client.Conn.WriteMessage(websocket.TextMessage, messageBytes)
if err != nil {
log.Error("Write error!", "reason", err)
return
}
}
func SendCmd(conn *websocket.Conn, pwd string, cmd string) {
func SendCmd(client ChronoTomato.Client, pwd string, cmd string) {
message := GoTomato.ClientCommand{
Command: cmd,
Password: pwd,
}
sendClientCommand(conn, message)
sendClientCommand(client, message)
}
func Send_updateSettings(conn *websocket.Conn, pwd string, settings GoTomato.PomodoroConfig) {
func Send_updateSettings(client ChronoTomato.Client, pwd string, settings GoTomato.PomodoroConfig) {
message := GoTomato.ClientCommand{
Command: "updateSettings",
Password: pwd,
Settings: settings,
}
sendClientCommand(conn, message)
sendClientCommand(client, message)
}