ChronoTomato/internal/websocket/send.go

46 lines
1 KiB
Go
Raw Normal View History

2024-10-22 11:13:41 +00:00
package websocket
import (
"encoding/json"
"github.com/gorilla/websocket"
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
2024-10-22 11:13:41 +00:00
)
// Sends a message to the clients websocket
func sendClientCommand(c Client, msg GoTomato.ClientCommand) {
2024-10-22 11:13:41 +00:00
messageBytes, err := json.Marshal(msg)
if err != nil {
helper.Logger.Error("Error marshalling!", "reason", err)
2024-10-22 11:13:41 +00:00
return
}
err = c.Conn.WriteMessage(websocket.TextMessage, messageBytes)
2024-10-22 11:13:41 +00:00
if err != nil {
helper.Logger.Error("Write error!", "reason", err)
2024-10-22 11:13:41 +00:00
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
}
sendClientCommand(c, 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
}
sendClientCommand(c, message)
2024-10-22 11:13:41 +00:00
}