-
Notifications
You must be signed in to change notification settings - Fork 0
/
App.js
54 lines (44 loc) · 1.19 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import React, { useState, useEffect } from 'react';
import io from 'socket.io-client';
const socket = io('http://localhost:3001');
function App() {
const [status, setStatus] = useState(Array(8).fill(false));
const [connected, setConnected] = useState(false);
useEffect(() => {
socket.on('connect', () => {
setConnected(true);
});
socket.on('disconnect', () => {
setConnected(false);
});
socket.on('status', (data) => {
setStatus(data);
});
const intervalId = setInterval(() => {
socket.emit('get-status');
}, 1000);
return () => {
clearInterval(intervalId);
};
}, []);
const handleButtonClick = (index) => {
const newStatus = [...status];
newStatus[index] = !newStatus[index];
setStatus(newStatus);
socket.emit('set-antenna', index, newStatus[index]);
};
return (
<div>
<h1>Antenna Control</h1>
{connected ? <p>Status: {status.join(',')}</p> : <p>Not connected</p>}
<div>
{status.map((s, i) => (
<button key={i} onClick={() => handleButtonClick(i)}>
{s ? 'On' : 'Off'}
</button>
))}
</div>
</div>
);
}
export default App;