Sebastian Mark
bdfd5c3b84
- remove unused Mutex from `models.Client` struct
- remove lock from `HandleConnection()`
- replace websocket.Conn with models.Client in `handleClientCommands()`
🤖
29 lines
857 B
Go
29 lines
857 B
Go
package models
|
|
|
|
import (
|
|
"github.com/charmbracelet/log"
|
|
"github.com/gorilla/websocket"
|
|
)
|
|
|
|
// Represents a command from the client (start/stop)
|
|
type ClientCommand struct {
|
|
Command string `json:"command"` // Command send to the server
|
|
Password string `json:"password"` // Pomodoro control password
|
|
Settings PomodoroConfig `json:"settings"` // Pomodoro config
|
|
}
|
|
|
|
// Represents a single client
|
|
type Client struct {
|
|
Conn *websocket.Conn
|
|
}
|
|
|
|
// 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 *Client) 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
|
|
}
|