ChronoTomato/internal/websocket/send.go

49 lines
979 B
Go
Raw Normal View History

2024-10-22 11:13:41 +00:00
package websocket
import (
"encoding/json"
"github.com/gorilla/websocket"
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) {
if !c.Connected() {
// noop if client is not connected
return
}
2024-10-22 11:13:41 +00:00
messageBytes, err := json.Marshal(msg)
if err != nil {
c.LastErr = 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 {
c.LastErr = 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
}