-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsocket.js
47 lines (41 loc) · 1004 Bytes
/
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
const SA = require("./constants");
const CONNECTION = (socket, io) => {
let { code, name = "Anonymous" } = socket.handshake.query;
socket.join(code);
socket.emit(
SA.DISCOVERY,
Object.keys(io.sockets.adapter.rooms[code].sockets).filter(
(socketID) => socketID != socket.id
)
);
socket.to(code).emit(SA.NOTIFICATION, {
from: socket.id,
senderName: name,
text: `${name} Joined`,
eventType: "connect",
});
socket.on(SA.OFFER, (data) => {
io.to(data.id).emit(SA.OFFER, { ...data, id: socket.id, name });
});
socket.on(SA.ANSWER, (data) => {
io.to(data.id).emit(SA.ANSWER, { ...data, id: socket.id, name });
});
socket.on(SA.CHAT_MESSAGE, (data) => {
socket.to(code).emit(SA.CHAT_MESSAGE, {
from: socket.id,
senderName: name,
...data,
});
});
socket.on("disconnect", () => {
io.to(code).emit(SA.NOTIFICATION, {
from: socket.id,
senderName: name,
text: `${name} Left`,
eventType: "disconnect",
});
});
};
module.exports = {
CONNECTION,
};