GoTomato/pkg/models/client.go
Sebastian Mark cb6616f400 break: add updateSettings command to modify Pomodoro settings
- add `updateSettings` command to modify Pomodoro configuration
- remove ability to set Pomodoro configuration in `start` command
- update demo client
- update README

🤖
2024-10-22 08:51:22 +02:00

32 lines
887 B
Go

package models
import (
"github.com/gorilla/websocket"
"log"
"sync"
)
// ClientCommand represents a command from the client (start/stop).
type ClientCommand struct {
Command string `json:"command"` // comman send to the server
Password string `json:"password"` // pomodoro control password
PomodoroSettings GoTomatoPomodoroConfig `json:"settings"` // pomodoro config
}
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
}