-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
76 lines (66 loc) · 2.16 KB
/
test.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
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
import express from 'express';
import { createServer } from 'http';
import { Server } from 'socket.io';
const app = express();
const server = createServer(app);
const io = new Server(server, {
cors: {
origin: '*',
},
});
const users = {};
// const groups = {};
io.on('connection', (socket) => {
console.log(`User connected: ${socket.id}`);
// Listen for 'login' event and store user with socket ID
socket.on('login', ({id}) => {
users[id] = socket.id
console.log(`User logged in: ${id}`);
io.emit("login", socket.id)
console.log("users : ",users)
});
// Handle private messages
socket.on('private-message', ({id , message}) => {
console.log(`Sending private message to User ${id} to ${message}`);
if (users[id]) {
console.log("users : ",users)
io.to(users[id]).emit('private-message', {
senderId: id,
message
});
io.to(socket.id).emit('private-message', {
senderId: id,
message
});
console.log(users[id])
} else {
console.log(`User ${id} not found or offline.`);
}
});
// Join a user to a group
socket.on('join_group', ({ userId, groupId }) => {
socket.join(groupId)
// Add user to group
console.log(`User ${userId} joined group ${groupId}`);
io.to(groupId).emit("join_group", `${userId + "this user is joined group" + groupId}`)
});
// Handle group messages
socket.on('group_message', ({ groupId, senderId, message }) => {
socket.join(groupId)
io.to(groupId).emit('group_message', { senderId, message });
console.log(`Message in group ${groupId} from ${senderId}: ${message}`);
});
// Handle user disconnection and clean up
socket.on('disconnect', () => {
console.log(`User disconnected: ${socket.id}`);
for (const [id] of Object.entries(users)) {
console.log("des : user id ", id)
if (id) {
delete users[id];
break;
}
}
console.log(users)
});
});
});