-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.js
More file actions
87 lines (74 loc) · 2.42 KB
/
Copy pathserver.js
File metadata and controls
87 lines (74 loc) · 2.42 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
const WebSocket = require('ws');
const wss = new WebSocket.Server({ port: 8080 });
const users = new Map(); // Map of user IDs to { ws, username, city, group }
const groups = new Set(); // Set of group names
wss.on('connection', (ws) => {
console.log('New client connected');
ws.on('message', (message) => {
let data;
try {
data = JSON.parse(message);
} catch (err) {
console.error('Invalid message format:', message);
return;
}
const userId = ws._id || Date.now().toString();
ws._id = userId;
if (data.type === 'join') {
// Create a default group for the user's city if it doesn't exist
const defaultGroup = `${data.city} Group`;
groups.add(defaultGroup);
users.set(userId, { ws, username: data.username, city: data.city, group: data.group });
// Broadcast updated user list
const userList = Array.from(users.values()).map(user => ({
username: user.username,
city: user.city,
}));
broadcast({ type: 'users', users: userList });
// Broadcast updated group list
broadcast({ type: 'groups', groups: Array.from(groups) });
} else if (data.type === 'createGroup') {
groups.add(data.groupName);
broadcast({ type: 'groups', groups: Array.from(groups) });
} else if (data.type === 'message') {
const user = users.get(userId);
if (user) {
const message = {
type: 'message',
username: data.username,
text: data.text,
city: data.city,
group: data.group,
timestamp: data.timestamp,
};
// Broadcast message to users in the same group
users.forEach((client, id) => {
if (
client.ws.readyState === WebSocket.OPEN &&
client.group === data.group
) {
client.ws.send(JSON.stringify(message));
}
});
}
}
});
ws.on('close', () => {
users.delete(ws._id);
// Broadcast updated user list
const userList = Array.from(users.values()).map(user => ({
username: user.username,
city: user.city,
}));
broadcast({ type: 'users', users: userList });
console.log('Client disconnected');
});
});
function broadcast(data) {
wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify(data));
}
});
}
console.log('WebSocket server running on ws://localhost:8080');