A robot interaction page to send and receive messages from the robot #10

Merged
0950726 merged 6 commits from feat/ui2cb-communication into dev 2025-10-08 14:01:03 +00:00
Showing only changes of commit c577daa9e6 - Show all commits

View File

@@ -1,35 +1,55 @@
import { useState } from 'react' import { useState, useEffect } from 'react'
import reactLogo from './assets/react.svg'
import viteLogo from '/vite.svg'
import './App.css' import './App.css'
function App() { function App() {
const [count, setCount] = useState(0) const [message, setMessage] = useState('');
const [sseMessage, setSseMessage] = useState('');
const sendMessage = async () => {
try {
const response = await fetch("http://localhost:8000/message", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ message }),
});
const data = await response.json();
console.log(data);
} catch (error) {
console.error("Error sending message: ", error);
}
};
useEffect(() => {
const eventSource = new EventSource("http://localhost:8000/sse");
eventSource.onmessage = (event) => {
setSseMessage(event.data);
};
return () => {
eventSource.close();
};
});
return ( return (
<> <div className="App">
<div> <div>
<a href="https://vite.dev" target="_blank"> <input
<img src={viteLogo} className="logo" alt="Vite logo" /> type="text"
</a> value={message}
<a href="https://react.dev" target="_blank"> onChange={(e) => setMessage(e.target.value)}
<img src={reactLogo} className="logo react" alt="React logo" /> placeholder="Enter a message"
</a> />
<button onClick={sendMessage}>Send Message to Backend</button>
</div> </div>
<h1>Vite + React</h1> <div>
<div className="card"> <h2>Message from Server (SSE):</h2>
<button onClick={() => setCount((count) => count + 1)}> <p>{sseMessage}</p>
count is {count}
</button>
<p>
Edit <code>src/App.tsx</code> and save to test HMR
</p>
</div> </div>
<p className="read-the-docs"> </div>
Click on the Vite and React logos to learn more );
</p>
</>
)
} }
export default App export default App