-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
61 lines (52 loc) · 1.9 KB
/
server.js
File metadata and controls
61 lines (52 loc) · 1.9 KB
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
const WebSocket = require('ws');
const readline = require('readline');
// Create a new WebSocket server
const wss = new WebSocket.Server({ port: 9080 });
// Function to handle new connections
wss.on('connection', (ws) => {
console.log('New client connected');
// Send a welcome message to the new client
// ws.send(JSON.stringify({ message: 'Welcome to the WebSocket server!' }));
ws.send(JSON.stringify({ message: 'Type messages in the terminal to send to all connected clients.' }));
ws.send(JSON.stringify({ message: 'Type "exit" to close the server.' }));
// Function to handle incoming messages
ws.on('message', (message) => {
try {
// Parse the received message as JSON
const data = JSON.parse(message);
console.log('Received data:', data);
// Handle the parsed data and send a response back to the client
if (data.type === 'greeting') {
ws.send(JSON.stringify({ message: `Hello, ${data.name}` }));
} else {
ws.send(JSON.stringify({ message: 'Unknown message type' }));
}
} catch (error) {
console.error('Error parsing message:', error);
ws.send(JSON.stringify({ error: 'Invalid JSON' }));
}
});
// Function to handle client disconnections
ws.on('close', () => {
console.log('Client disconnected');
});
});
// Function to broadcast messages to all connected clients
function broadcast(data) {
wss.clients.forEach((client) => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(data));
}
});
}
// Set up terminal input listener
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.on('line', (input) => {
const newData = { message: input, timestamp: new Date() };
broadcast(newData);
});
console.log('WebSocket server is running on ws://localhost:8080');
console.log('Type messages in the terminal to send to all connected clients.');