Compare commits

..

No commits in common. "64050cd1d2d30e3c16e73c7c7dd1a1c7df64f9aa" and "90ce4a514b1cf27744af9b9ed2847bab487bf301" have entirely different histories.

2 changed files with 37 additions and 127 deletions

View file

@ -4,7 +4,6 @@ import (
"encoding/json" "encoding/json"
"log" "log"
"net/http" "net/http"
"sync"
"time" "time"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
@ -24,14 +23,7 @@ type BroadcastMessage struct {
TimeLeft int `json:"time_left"` TimeLeft int `json:"time_left"`
} }
type ClientCommand struct {
Command string `json:"command"`
}
var clients = make(map[*websocket.Conn]bool) 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
// broadcastMessage sends the remaining time to all connected clients. // broadcastMessage sends the remaining time to all connected clients.
func broadcastMessage(message BroadcastMessage) { func broadcastMessage(message BroadcastMessage) {
@ -46,54 +38,37 @@ func broadcastMessage(message BroadcastMessage) {
} }
} }
// startTimer runs the countdown and broadcasts every second. // start a countdown and broadcast ever second
func startTimer(remainingSeconds int, mode string, session int) bool { func startTimer(remaining_seconds int, mode string, session int) {
for remainingSeconds > 0 { for remaining_seconds > 0 {
select { broadcastMessage(BroadcastMessage{
case <-timerStopChannel: Mode: mode,
return false // Stop the timer if a stop command is received Session: session,
default: MaxSession: sessions,
broadcastMessage(BroadcastMessage{ TimeLeft: remaining_seconds,
Mode: mode, })
Session: session, time.Sleep(time.Second)
MaxSession: sessions, remaining_seconds--
TimeLeft: remainingSeconds,
})
time.Sleep(time.Second)
remainingSeconds--
}
} }
// send final 0 timer
broadcastMessage(BroadcastMessage{ broadcastMessage(BroadcastMessage{
Mode: mode, Mode: mode,
Session: session, Session: 1,
MaxSession: sessions, MaxSession: 4,
TimeLeft: 0, TimeLeft: 0,
}) })
return true
} }
// runPomodoroTimer iterates the Pomodoro work/break sessions. // iterate the Pomodoro work/break sessions
func runPomodoroTimer() { func runPomodoroTimer() {
mu.Lock()
timerRunning = true
for session := 1; session <= sessions; session++ { for session := 1; session <= sessions; session++ {
if !startTimer(workDuration, "Work", session) { startTimer(workDuration, "Work", session)
break
}
if session == sessions { if session == sessions {
if !startTimer(longBreakDuration, "LongBreak", session) { startTimer(longBreakDuration, "LongBreak", session)
break
}
} else { } else {
if !startTimer(shortBreakDuration, "ShortBreak", session) { startTimer(shortBreakDuration, "ShortBreak", session)
break
}
} }
} }
timerRunning = false
mu.Unlock()
} }
// upgrade HTTP requests to WebSocket connections. // upgrade HTTP requests to WebSocket connections.
@ -113,40 +88,23 @@ func handleConnections(w http.ResponseWriter, r *http.Request) {
// Register the new client // Register the new client
clients[ws] = true clients[ws] = true
// Listen for commands from this client // Clean up when the client disconnects
for { for {
_, message, err := ws.ReadMessage() _, _, err := ws.ReadMessage()
if err != nil { if err != nil {
log.Printf("Client disconnected: %v", err) log.Printf("Client disconnected: %v", err)
delete(clients, ws) delete(clients, ws)
break 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
}
}
} }
} }
func main() { func main() {
http.HandleFunc("/ws", handleConnections) http.HandleFunc("/ws", handleConnections)
// Start a goroutine that runs the Pomodoro timer and broadcasts updates.
go runPomodoroTimer()
log.Println("Pomodoro WebSocket server started on :8080") log.Println("Pomodoro WebSocket server started on :8080")
err := http.ListenAndServe(":8080", nil) err := http.ListenAndServe(":8080", nil)
if err != nil { if err != nil {

View file

@ -1,72 +1,24 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Pomodoro Timer</title> <title>Pomodoro Timer</title>
<style>
button {
padding: 10px 20px;
margin: 5px;
font-size: 16px;
}
#timer {
font-size: 24px;
margin-top: 20px;
}
</style>
</head> </head>
<body> <body>
<h1>Pomodoro Timer</h1> <h1>Pomodoro Timer</h1>
<div id="timer">Connecting to server...</div> <div id="timer">Connecting to server...</div>
<!-- Buttons to start and stop the timer --> <script>
<button id="startButton">Start</button> var ws = new WebSocket("ws://localhost:8080/ws");
<button id="stopButton">Stop</button>
<script> ws.onmessage = function(event) {
var ws = new WebSocket("ws://localhost:8080/ws"); document.getElementById("timer").innerText = event.data;
};
ws.onopen = function () { ws.onclose = function() {
document.getElementById("timer").innerText = "Connected to server."; document.getElementById("timer").innerText = "Connection closed.";
}; };
</script>
// Handle incoming messages and update the timer display
ws.onmessage = function (event) {
var data = JSON.parse(event.data);
var mode = data.mode;
var session = data.session;
var maxSession = data.max_session;
var timeLeft = data.time_left;
document.getElementById("timer").innerText =
mode + " Session " + session + "/" + maxSession + ": " + formatTime(timeLeft);
};
ws.onclose = function () {
document.getElementById("timer").innerText = "Connection closed.";
};
// Format time in MM:SS
function formatTime(seconds) {
var minutes = Math.floor(seconds / 60);
var remainingSeconds = seconds % 60;
return minutes.toString().padStart(2, '0') + ":" + remainingSeconds.toString().padStart(2, '0');
}
// Send start command to the server
document.getElementById("startButton").addEventListener("click", function () {
ws.send(JSON.stringify({command: "start"}));
});
// Send stop command to the server
document.getElementById("stopButton").addEventListener("click", function () {
ws.send(JSON.stringify({command: "stop"}));
});
</script>
</body> </body>
</html> </html>