GoTomato/pkg/models/client.go

31 lines
892 B
Go
Raw Normal View History

package models
import (
"github.com/charmbracelet/log"
"github.com/gorilla/websocket"
)
2024-10-30 06:37:14 +00:00
// Represents a command from the client (start/stop)
type ClientCommand struct {
2024-10-30 06:37:14 +00:00
Command string `json:"command"` // Command send to the server
Password string `json:"password"` // Pomodoro control password
Settings PomodoroConfig `json:"settings"` // Pomodoro config
}
2024-10-30 06:37:14 +00:00
// Represents a single client
type WebsocketClient struct {
Conn *websocket.Conn
RealIP string
}
2024-10-30 06:37:14 +00:00
// Sends a message to the websocket.
// Automatically locks and unlocks the client mutex, to ensure that only one goroutine can write at a time.
func (c *WebsocketClient) SendMessage(messageType int, data []byte) error {
err := c.Conn.WriteMessage(messageType, data)
if err != nil {
log.Error("Error writing to WebSocket:", "msg", err)
c.Conn.Close() // Close the connection on error
}
return err
}