This repository has been archived on 2025-07-16. You can view files and clone it, but cannot push or open issues or pull requests.
Files
vue-with-websockets/backend/index.js
2025-07-07 15:52:32 +03:00

27 lines
762 B
JavaScript

import { WebSocketServer } from 'ws';
import HistoryFile from './classes/HistoryFile.js';
// Define WebSocket server
const wss = new WebSocketServer({ port: 8080 });
// Define History file class
// For managing data state beyond restart
const history = new HistoryFile('data.json');
wss.on('connection', (ws) => {
// On successful connection, we send history data to the client
// After that, we procceed sending data update every 10 seconds
console.log('Client connected');
ws.on('message', (message) => {
console.log(`Received message => ${message}`);
});
ws.on('close', () => {
console.log('Client disconnected');
});
ws.send('Hello! Message from server.');
});
console.log('WebSocket server is running on ws://localhost:8080');