diff --git a/react/src/app.jsx b/react/src/app.jsx index a5a00de..d92a093 100644 --- a/react/src/app.jsx +++ b/react/src/app.jsx @@ -1,24 +1,14 @@ import React from "react"; -function App(){ - const [message, setMessage] = React.useState(''); +function App() { const [alerts, setAlerts] = React.useState([]); const ws = React.useRef(null); const timeout = React.useRef(null); - const handleChange = e => { - setMessage(e.target.value); - } - - const sendMessage = e => { - e.preventDefault(); - + const sendMessage = (message) => { if (ws.current.readyState != 1) { return false; } - if (!message) { - return false; - } let msg = JSON.stringify({ text: message, @@ -28,7 +18,8 @@ function App(){ ws.current.send(msg); addAlert(msg); - setMessage(''); + + return true; } const addAlert = (msg) => { @@ -44,11 +35,11 @@ function App(){ // ws.current.send("Hello from the client!"); } ws.current.onclose = () => console.log('Disconnected'); + ws.current.onerror = () => console.log('Websocket error') return () => { ws.current.close(); } - }, []); // Sets a received message handler, @@ -60,7 +51,6 @@ function App(){ const msg = e.data; addAlert(msg); } - }, [alerts]); // Clears out alerts with a delay, @@ -73,7 +63,6 @@ function App(){ return () => { clearTimeout(timeout.current); } - }, [alerts]); return ( @@ -81,16 +70,7 @@ function App(){