-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.js
51 lines (39 loc) · 1.23 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
const io = require("socket.io")(8800, {
cors: {
origin: "http://localhost:3000"
}
});
let activeUsers = [];
console.log("front");
io.on("connection", socket => {
console.log("in")
socket.on("new-user", newUserId => {
if(!activeUsers.some(user => user.id === newUserId)){
activeUsers.push({
id: newUserId,
socketId: socket.id
});
}
console.log("connected users: ", activeUsers);
io.emit("get-users", activeUsers);
});
console.log("connected users: ", activeUsers);
socket.on("--x", () => {
console.log("--x");
});
socket.on("send-message", data => {
const { receiverId } = data;
const user = activeUsers.find(user => user.userId === receiverId);
console.log("Sending from socket to: " + receiverId);
console.log("data: "+ data);
if (user) {
io.to(user.socketId).emit("receive-message", data);
}
})
socket.on("disconnect", () => {
activeUsers = activeUsers.filter(user => user.socketId !== socket.id);
console.log("User disconnected!", activeUsers);
io.emit("get-users", activeUsers);
});
});
console.log("orrrr");