ChronoTomato/internal/websocket/send.go
Sebastian Mark 561419b568 feat: implement centralized logging helper
- remove direct log usage
- create a new logging helper
- update all log calls to use the new Logger instance
- set timestamp to ISO8601

🤖
2024-11-04 20:39:21 +01:00

45 lines
1 KiB
Go

package websocket
import (
"encoding/json"
"github.com/gorilla/websocket"
"git.smsvc.net/pomodoro/ChronoTomato/internal/helper"
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
)
// Sends a message to the clients websocket
func sendClientCommand(c Client, msg GoTomato.ClientCommand) {
messageBytes, err := json.Marshal(msg)
if err != nil {
helper.Logger.Error("Error marshalling!", "reason", err)
return
}
err = c.Conn.WriteMessage(websocket.TextMessage, messageBytes)
if err != nil {
helper.Logger.Error("Write error!", "reason", err)
return
}
}
// Sends the passed command to the server
func (c Client) SendCmd(cmd string) {
message := GoTomato.ClientCommand{
Command: cmd,
Password: c.Password,
}
sendClientCommand(c, message)
}
// Sends the new PomodoroConfig to the server
func (c Client) SendSettingsUpdate(settings GoTomato.PomodoroConfig) {
message := GoTomato.ClientCommand{
Command: "updateSettings",
Password: c.Password,
Settings: settings,
}
sendClientCommand(c, message)
}