ChronoTomato/internal/websocket/send.go

46 lines
1,000 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"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
2024-10-22 11:13:41 +00:00
)
2024-10-30 10:03:18 +00:00
// Sends to ClientCommand to the clients websocket
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
}
}
2024-10-30 10:03:18 +00:00
// Sends the passed command to the server
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
}
2024-10-30 10:03:18 +00:00
// Sends the new PomodoroConfig to the server
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
}