feat: refactor client handling and introduce websocket client type
- 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
🤖
This commit is contained in:
parent
b943c9d6eb
commit
0ca90b50a5
6 changed files with 25 additions and 18 deletions
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
8
pkg/models/client.go
Normal file
8
pkg/models/client.go
Normal file
|
@ -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
|
||||
}
|
Loading…
Reference in a new issue