2024-10-22 11:13:41 +00:00
|
|
|
package websocket
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"github.com/gorilla/websocket"
|
2024-10-27 20:34:53 +00:00
|
|
|
|
2024-11-03 21:22:03 +00:00
|
|
|
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
|
2024-10-27 20:34:53 +00:00
|
|
|
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
2024-10-22 11:13:41 +00:00
|
|
|
)
|
|
|
|
|
2024-10-30 18:29:40 +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 {
|
2024-11-03 21:22:03 +00:00
|
|
|
helper.Logger.Error("Error marshalling!", "reason", err)
|
2024-10-22 11:13:41 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-30 07:54:58 +00:00
|
|
|
err = c.Conn.WriteMessage(websocket.TextMessage, messageBytes)
|
2024-10-22 11:13:41 +00:00
|
|
|
if err != nil {
|
2024-11-03 21:22:03 +00:00
|
|
|
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
|
2024-10-30 08:00:29 +00:00
|
|
|
func (c Client) SendCmd(cmd string) {
|
2024-10-27 09:41:31 +00:00
|
|
|
message := GoTomato.ClientCommand{
|
2024-10-22 11:13:41 +00:00
|
|
|
Command: cmd,
|
2024-10-30 08:00:29 +00:00
|
|
|
Password: c.Password,
|
2024-10-22 11:13:41 +00:00
|
|
|
}
|
|
|
|
|
2024-10-30 18:29:40 +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
|
2024-10-30 08:00:29 +00:00
|
|
|
func (c Client) SendSettingsUpdate(settings GoTomato.PomodoroConfig) {
|
2024-10-27 09:41:31 +00:00
|
|
|
message := GoTomato.ClientCommand{
|
2024-10-28 06:56:37 +00:00
|
|
|
Command: "updateSettings",
|
2024-10-30 08:00:29 +00:00
|
|
|
Password: c.Password,
|
2024-10-28 06:56:37 +00:00
|
|
|
Settings: settings,
|
2024-10-22 11:13:41 +00:00
|
|
|
}
|
|
|
|
|
2024-10-30 18:29:40 +00:00
|
|
|
sendClientCommand(c, message)
|
2024-10-22 11:13:41 +00:00
|
|
|
}
|