ChronoTomato/internal/websocket/send.go

46 lines
989 B
Go
Raw Normal View History

2024-10-22 11:13:41 +00:00
package websocket
import (
"encoding/json"
"github.com/charmbracelet/log"
"github.com/gorilla/websocket"
ChronoTomato "git.smsvc.net/pomodoro/ChronoTomato/pkg/models"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
2024-10-22 11:13:41 +00:00
)
type Client ChronoTomato.GoTomatoClient // New websocket client
func (c Client) sendClientCommand(msg GoTomato.ClientCommand) {
2024-10-22 11:13:41 +00:00
messageBytes, err := json.Marshal(msg)
if err != nil {
log.Error("Error marshalling!", "reason", err)
return
}
err = c.Conn.WriteMessage(websocket.TextMessage, messageBytes)
2024-10-22 11:13:41 +00:00
if err != nil {
log.Error("Write error!", "reason", err)
return
}
}
func (c Client) SendCmd(cmd string) {
message := GoTomato.ClientCommand{
2024-10-22 11:13:41 +00:00
Command: cmd,
Password: c.Password,
2024-10-22 11:13:41 +00:00
}
c.sendClientCommand(message)
2024-10-22 11:13:41 +00:00
}
func (c Client) SendSettingsUpdate(settings GoTomato.PomodoroConfig) {
message := GoTomato.ClientCommand{
Command: "updateSettings",
Password: c.Password,
Settings: settings,
2024-10-22 11:13:41 +00:00
}
c.sendClientCommand(message)
2024-10-22 11:13:41 +00:00
}