2024-10-20 09:06:37 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"log"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ClientCommand represents a command from the client (start/stop).
|
|
|
|
type ClientCommand struct {
|
2024-10-21 13:36:26 +00:00
|
|
|
Command string `json:"command"` // comman send to the server
|
|
|
|
Password string `json:"password"` // pomodoro control password
|
|
|
|
Config GoTomatoPomodoroConfig `json:"config"` // 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 {
|
|
|
|
log.Printf("Error writing to WebSocket: %v", err)
|
|
|
|
c.Conn.Close() // Close the connection on error
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|