-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
33 lines (26 loc) · 953 Bytes
/
server.js
File metadata and controls
33 lines (26 loc) · 953 Bytes
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
const dgram = require('dgram');
const express = require('express');
const cors = require('cors');
const app = express(); // Initialize the app first
const PORT = 3001;
// Enable CORS so your frontend can access this server
app.use(cors({ origin: '*' }));
// Create a UDP client
const udpClient = dgram.createSocket('udp4');
// Define an endpoint to handle the stop signal
app.post('/send-stop-signal', (req, res) => {
const stopSignal = Buffer.from([1]); // 1 is the stop signal
console.log(stopSignal);
// Send the UDP stop signal to the RSU
udpClient.send(stopSignal, 44002, 'localhost', (err) => {
if (err) {
console.error('Error sending UDP signal:', err);
return res.status(500).send('Failed to send stop signal');
}
res.send('Stop signal sent successfully');
});
});
// Start the server
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});