Sebastian Mark
05e8bf5854
- add `Connected()` method for client - add `LastErr` field to GoTomatoClient - replace error logging by updating `LastErr` field - modify methods to use pointer receivers for Client - add `prevMessage` variable to store the last received server message - show connect status in TUI - use EnterAltScreen instead of ClearScreen
48 lines
979 B
Go
48 lines
979 B
Go
package websocket
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/gorilla/websocket"
|
|
|
|
GoTomato "git.smsvc.net/pomodoro/GoTomato/pkg/models"
|
|
)
|
|
|
|
// Sends a message to the clients websocket
|
|
func sendClientCommand(c *Client, msg GoTomato.ClientCommand) {
|
|
if !c.Connected() {
|
|
// noop if client is not connected
|
|
return
|
|
}
|
|
messageBytes, err := json.Marshal(msg)
|
|
if err != nil {
|
|
c.LastErr = err
|
|
return
|
|
}
|
|
|
|
err = c.Conn.WriteMessage(websocket.TextMessage, messageBytes)
|
|
if err != nil {
|
|
c.LastErr = 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)
|
|
}
|