-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
63 lines (52 loc) · 1.59 KB
/
server.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
55
56
57
58
59
60
61
62
63
const express = require('express');
const WebSocket = require('ws');
const http = require('http');
const fs = require('fs');
const path = require('path');
const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
const csvFilePath = path.join(__dirname, 'sheet.csv');
app.use(express.static(path.join(__dirname, 'public')));
// Variable to store the data that's already in the csv
let initialCsvData = '';
// Function to send data to all connected clients
function sendDataToClients(data) {
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(data);
}
});
}
fs.readFile(csvFilePath, 'utf8', (err, data) => {
if (err) {
console.error(err);
} else {
initialCsvData = data;
sendDataToClients(data);
}
});
wss.on('connection', (ws) => {
console.log('Client connected');
if (initialCsvData) {
ws.send(initialCsvData); //Send the data that's already in the csv to the client
}
ws.on('close', () => {
console.log('Client disconnected');
});
});
// Watch the CSV data file for new data or any changes
fs.watch(csvFilePath, (filename) => {
if (filename) {
fs.readFile('sheet.csv', 'utf8', (err, data) => {
if (err) {
console.error(err);
return;
}
sendDataToClients(data); // Send the new data through the WebSocket to update the dashboard
});
}
});
server.listen(3000, () => {
console.log('Running server on http://localhost:3000');
});