2024-10-20 09:06:37 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2024-10-25 22:01:07 +00:00
|
|
|
"github.com/charmbracelet/log"
|
2024-10-20 09:06:37 +00:00
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ClientCommand represents a command from the client (start/stop).
|
|
|
|
type ClientCommand struct {
|
2024-10-26 07:16:48 +00:00
|
|
|
Command string `json:"command"` // comman send to the server
|
|
|
|
Password string `json:"password"` // pomodoro control password
|
|
|
|
Settings GoTomatoPomodoroConfig `json:"settings"` // pomodoro config
|
2024-10-20 09:06:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
Conn *websocket.Conn
|
|
|
|
Mutex sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// It automatically locks and unlocks the mutex to ensure that only one goroutine can write at a time.
|
|
|
|
func (c *Client) SendMessage(messageType int, data []byte) error {
|
|
|
|
c.Mutex.Lock()
|
|
|
|
defer c.Mutex.Unlock()
|
|
|
|
|
|
|
|
err := c.Conn.WriteMessage(messageType, data)
|
|
|
|
if err != nil {
|
2024-10-25 22:01:07 +00:00
|
|
|
log.Error("Error writing to WebSocket:", "msg", err)
|
2024-10-20 09:06:37 +00:00
|
|
|
c.Conn.Close() // Close the connection on error
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|