GoTomato/pkg/models/client.go
Sebastian Mark 52634ed226 feat: allow clients to send pomodoro config
- allow clients to send custom configuration for pomodoro sessions
- update RunPomodoro to accept a configuration parameter
- modify startTimer to handle session count from config
- add default pomodoro configuration in client command handling

🤖
2024-10-20 23:18:35 +02:00

31 lines
730 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"`
Config GoTomatoPomodoroConfig `json:"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
}