2024-10-20 09:06:37 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2024-11-20 18:53:40 +00:00
|
|
|
"time"
|
2024-11-04 19:33:25 +00:00
|
|
|
|
2024-11-20 18:53:40 +00:00
|
|
|
"github.com/gorilla/websocket"
|
2024-10-20 09:06:37 +00:00
|
|
|
)
|
|
|
|
|
2024-11-20 18:53:40 +00:00
|
|
|
const TIMEOUT = 10
|
|
|
|
|
2024-10-30 06:37:14 +00:00
|
|
|
// Represents a command from the client (start/stop)
|
2024-10-20 09:06:37 +00:00
|
|
|
type ClientCommand struct {
|
2024-10-30 06:37:14 +00:00
|
|
|
Command string `json:"command"` // Command send to the server
|
|
|
|
Password string `json:"password"` // Pomodoro control password
|
|
|
|
Settings PomodoroConfig `json:"settings"` // Pomodoro config
|
2024-10-20 09:06:37 +00:00
|
|
|
}
|
|
|
|
|
2024-10-30 06:37:14 +00:00
|
|
|
// Represents a single client
|
2024-10-30 08:57:09 +00:00
|
|
|
type WebsocketClient struct {
|
2024-11-20 19:07:29 +00:00
|
|
|
Conn *websocket.Conn
|
|
|
|
LastPong time.Time
|
|
|
|
RealIP string
|
2024-10-20 09:06:37 +00:00
|
|
|
}
|
|
|
|
|
2024-10-30 06:37:14 +00:00
|
|
|
// Sends a message to the websocket.
|
|
|
|
// Automatically locks and unlocks the client mutex, to ensure that only one goroutine can write at a time.
|
2024-10-30 08:57:09 +00:00
|
|
|
func (c *WebsocketClient) SendMessage(messageType int, data []byte) error {
|
2024-11-20 18:53:40 +00:00
|
|
|
c.Conn.SetWriteDeadline(time.Now().Add(TIMEOUT * time.Second))
|
2024-11-20 19:15:24 +00:00
|
|
|
c.Conn.WriteMessage(websocket.PingMessage, nil)
|
2024-11-20 19:07:29 +00:00
|
|
|
|
|
|
|
c.Conn.SetWriteDeadline(time.Now().Add(TIMEOUT * time.Second))
|
|
|
|
return c.Conn.WriteMessage(messageType, data)
|
2024-10-20 09:06:37 +00:00
|
|
|
}
|