TerminalX / server.js
Kano001's picture
Update server.js
ff4613e verified
raw
history blame
786 Bytes
const WebSocket = require('ws');
// Create a WebSocket server listening on port 8080
const wss = new WebSocket.Server({ port: 8080 });
// Event listener for new connections
wss.on('connection', (ws) => {
console.log('New client connected');
// Send a message to the client when they connect
ws.send('Welcome to the WebSocket server!');
// Event listener for messages from the client
ws.on('message', (message) => {
console.log(`Received message: ${message}`);
// Echo the message back to the client
ws.send(`You said: ${message}`);
});
// Event listener for client disconnects
ws.on('close', () => {
console.log('Client disconnected');
});
});
console.log('WebSocket server is running on ws://localhost:8080');