feat: implement client start/stop commands
- add ClientCommand struct to handle incoming commands
- introduce timerStopChannel to manage timer stopping
- modify startTimer to return a boolean for success/failure
- update runPomodoroTimer to handle timer start/stop commands
- add start and stop buttons in the index.html for user interaction
🤖
This commit is contained in:
parent
fa4eebbe76
commit
6d73711341
2 changed files with 122 additions and 35 deletions
47
index.html
47
index.html
|
@ -5,22 +5,67 @@
|
|||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<title>Pomodoro Timer</title>
|
||||
<style>
|
||||
button {
|
||||
padding: 10px 20px;
|
||||
margin: 5px;
|
||||
font-size: 16px;
|
||||
}
|
||||
|
||||
#timer {
|
||||
font-size: 24px;
|
||||
margin-top: 20px;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<h1>Pomodoro Timer</h1>
|
||||
<div id="timer">Connecting to server...</div>
|
||||
|
||||
<!-- Buttons to start and stop the timer -->
|
||||
<button id="startButton">Start</button>
|
||||
<button id="stopButton">Stop</button>
|
||||
|
||||
<script>
|
||||
var ws = new WebSocket("ws://localhost:8080/ws");
|
||||
|
||||
ws.onopen = function () {
|
||||
document.getElementById("timer").innerText = "Connected to server.";
|
||||
};
|
||||
|
||||
// Handle incoming messages and update the timer display
|
||||
ws.onmessage = function (event) {
|
||||
document.getElementById("timer").innerText = event.data;
|
||||
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>
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue