GoTomato/GoTomato.go

156 lines
3.3 KiB
Go
Raw Normal View History

2024-10-18 22:39:29 +00:00
package main
import (
"encoding/json"
"log"
"net/http"
"sync"
2024-10-18 22:39:29 +00:00
"time"
"github.com/gorilla/websocket"
)
const (
workDuration = 15 * 60
shortBreakDuration = 5 * 60
longBreakDuration = 10 * 60
sessions = 4
)
type BroadcastMessage struct {
Mode string `json:"mode"`
Session int `json:"session"`
MaxSession int `json:"max_session"`
TimeLeft int `json:"time_left"`
}
type ClientCommand struct {
Command string `json:"command"`
}
2024-10-18 22:39:29 +00:00
var clients = make(map[*websocket.Conn]bool)
var timerRunning bool
var timerStopChannel = make(chan bool, 1)
var mu sync.Mutex // to synchronize access to shared state
2024-10-18 22:39:29 +00:00
// broadcastMessage sends the remaining time to all connected clients.
func broadcastMessage(message BroadcastMessage) {
jsonMessage, _ := json.Marshal(message)
for client := range clients {
err := client.WriteMessage(websocket.TextMessage, jsonMessage)
if err != nil {
log.Printf("Error broadcasting to client: %v", err)
client.Close()
delete(clients, client)
}
}
}
// startTimer runs the countdown and broadcasts every second.
func startTimer(remainingSeconds int, mode string, session int) bool {
for remainingSeconds > 0 {
select {
case <-timerStopChannel:
return false // Stop the timer if a stop command is received
default:
broadcastMessage(BroadcastMessage{
Mode: mode,
Session: session,
MaxSession: sessions,
TimeLeft: remainingSeconds,
})
time.Sleep(time.Second)
remainingSeconds--
}
2024-10-18 22:39:29 +00:00
}
broadcastMessage(BroadcastMessage{
Mode: mode,
Session: session,
MaxSession: sessions,
2024-10-18 22:39:29 +00:00
TimeLeft: 0,
})
return true
2024-10-18 22:39:29 +00:00
}
// runPomodoroTimer iterates the Pomodoro work/break sessions.
2024-10-18 22:39:29 +00:00
func runPomodoroTimer() {
mu.Lock()
timerRunning = true
2024-10-18 22:39:29 +00:00
for session := 1; session <= sessions; session++ {
if !startTimer(workDuration, "Work", session) {
break
}
2024-10-18 22:39:29 +00:00
if session == sessions {
if !startTimer(longBreakDuration, "LongBreak", session) {
break
}
2024-10-18 22:39:29 +00:00
} else {
if !startTimer(shortBreakDuration, "ShortBreak", session) {
break
}
2024-10-18 22:39:29 +00:00
}
}
timerRunning = false
mu.Unlock()
2024-10-18 22:39:29 +00:00
}
// upgrade HTTP requests to WebSocket connections.
var upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool { return true },
}
func handleConnections(w http.ResponseWriter, r *http.Request) {
// Upgrade initial GET request to a WebSocket
ws, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Printf("WebSocket upgrade error: %v", err)
return
}
defer ws.Close()
// Register the new client
clients[ws] = true
// Listen for commands from this client
2024-10-18 22:39:29 +00:00
for {
_, message, err := ws.ReadMessage()
2024-10-18 22:39:29 +00:00
if err != nil {
log.Printf("Client disconnected: %v", err)
delete(clients, ws)
break
}
// Handle incoming commands
var command ClientCommand
err = json.Unmarshal(message, &command)
if err != nil {
log.Printf("Error unmarshalling command: %v", err)
continue
}
// Process the commands
switch command.Command {
case "start":
if !timerRunning {
go runPomodoroTimer()
}
case "stop":
if timerRunning {
timerStopChannel <- true
}
}
2024-10-18 22:39:29 +00:00
}
}
func main() {
http.HandleFunc("/ws", handleConnections)
log.Println("Pomodoro WebSocket server started on :8080")
err := http.ListenAndServe(":8080", nil)
if err != nil {
log.Fatalf("Error starting server: %v", err)
}
}