-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.js
51 lines (44 loc) · 1.34 KB
/
socket.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
function SocketConnection(io) {
// io.use((socket, next) => {
// const token = socket.handshake.query.token;
// if (!token) {
// return next(new Error('Authentication token missing'));
// }
// jwt.verify(token, SECRET_KEY, (err, decoded) => {
// if (err) {
// return next(new Error('Authentication error'));
// }
// const user = users.find(u => u.id === decoded.userId);
// if (!user) {
// return next(new Error('User not found'));
// }
// socket.user = user;
// next();
// });
// });
io.on("connection", (socket) => {
console.log(`Connected to ${socket.id}`);
// joined chat
socket.on("join-chat", (data) => {
socket.join(data.roomId);
console.log(`Connected to ${socket.id}`);
});
// sent & receive message
socket.on("send-chat-message", (data) => {
socket.to(data.roomId).emit("received-chat-message", data);
});
// typing indicator
socket.on("typing", (data) => {
socket.to(data.roomId).emit("userTyping", data);
});
// stop-typing indicator
socket.on("stop typing", (data) => {
socket.to(data.roomId).emit("userStoppedTyping", data);
});
socket.on("disconnect", () => {
console.log("Disconnected to" + socket.id);
});
// end
});
}
module.exports = { SocketConnection };