-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtcp_server5 - Copy.js
47 lines (36 loc) · 1.46 KB
/
tcp_server5 - Copy.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
const WebSocket = require('ws');
const uuid = require('uuid'); // For generating unique identifiers
// Create a WebSocket server
const wss = new WebSocket.Server({ port: 8080 });
// Map to store connections with unique identifiers
const clients = new Map();
wss.on('connection', (ws) => {
// Assign a unique identifier to the connection
const clientId = uuid.v4(); // UUID for unique identification
clients.set(clientId, ws);
console.log(`Client connected with ID: ${clientId}`);
// Send the Client ID to the client
ws.send(JSON.stringify({ type: 'client-id', id: clientId, message: "CONNECTING..." }));
// Handle incoming messages
ws.on('message', (message) => {
console.log(`Received from client ${clientId}`);
const data = JSON.parse(message); // Assuming JSON messages
console.log(`Received data.type: [${data.type}]`);
console.log(`Received data.id: [${data.id}]`);
console.log(`Received data.message: [${data.message}]`);
console.log();
// Broadcast the message to all connected clients
clients.forEach((client, id) => {
if (id !== clientId) {
// Parse the string into a JSON object
const jsonData = JSON.stringify({ type: data.type, id: clientId, message: data.message });
client.send(jsonData);
}
});
});
// Handle client disconnections
ws.on('close', () => {
console.log(`Client ${clientId} disconnected`);
clients.delete(clientId); // Remove from the map
});
});