Files
ai_live_chatbot/server.js

48 lines
1.3 KiB
JavaScript

const express = require('express');
const path = require('path');
const http = require('http');
const WebSocket = require('ws');
const fs = require('fs');
const app = express();
const PORT = 3000;
app.use(express.static(path.join(__dirname, 'public')));
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
wss.on('connection', (ws, req) => {
const endpoint = req.url;
if (endpoint === '/handler1') {
const interval = setInterval(() => {
try {
const data = fs.readFileSync('/dev/shm/1.txt', 'utf8').trim();
ws.send(data);
} catch (e) {
ws.send('0');
}
}, 50); // sends every 50ms = 20 times per second
ws.on('close', () => {
clearInterval(interval); // cleanup when client disconnects
});
}
// if (endpoint === '/handler2') {
// const interval = setInterval(() => {
// try {
// const data = fs.readFileSync('/dev/shm/2.txt', 'utf8').trim();
// ws.send(data);
// } catch (e) {
// ws.send('0');
// }
// }, 50);
// ws.on('close', () => clearInterval(interval));
// }
});
server.listen(PORT, () => {
console.log(`Server running at http://localhost:${PORT}`);
});