-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
115 lines (100 loc) · 2.86 KB
/
server.js
File metadata and controls
115 lines (100 loc) · 2.86 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
const express = require("express");
const app = express();
const http = require("http");
const { Server } = require("socket.io");
const USER = require("./src/utils/constants/user");
const CODE = require("./src/utils/constants/code");
const path = require("path");
const server = http.createServer(app);
const io = new Server(server);
app.use(express.static("build"));
app.use((req, res, next) => {
res.sendFile(path.join(__dirname, "build", "index.html"));
});
const usersMap = {};
function getClientsInRoom(roomId) {
return Array.from(io.sockets.adapter.rooms.get(roomId) || []).map(
(socketId) => {
return {
socketId,
username: usersMap[socketId],
focus: true,
};
}
);
}
io.on("connection", (socket) => {
socket.on(USER.JOIN, ({ username, roomId }) => {
usersMap[socket.id] = username;
socket.join(roomId);
const clients = getClientsInRoom(roomId);
clients.forEach(({ socketId }) => {
io.to(socketId).emit(USER.JOINED, {
username,
clients,
socketId: socket.id,
});
});
});
socket.on(CODE.SYNC, ({ code, question, socketId, users, language }) => {
io.to(socketId).emit(CODE.CHANGE, { code, language });
io.to(socketId).emit(USER.QUESTON, { question });
});
socket.on(USER.QUESTON, ({ question, roomId }) => {
const clients = getClientsInRoom(roomId);
clients.forEach(({ socketId }) => {
if (socketId !== socket.id)
io.to(socketId).emit(USER.QUESTON, { question });
});
});
socket.on(CODE.CHANGE, ({ code, roomId }) => {
socket.in(roomId).emit(CODE.CHANGE, { code });
});
socket.on("FOCUS", ({ id, focus, roomId }) => {
socket.in(roomId).emit("FOCUS", {
id,
focus,
});
});
socket.on("LANGUAGE", ({ lang, roomId }) => {
socket.in(roomId).emit("LANGUAGE", {
lang,
});
});
socket.on(USER.MESSAGE, ({ message, roomId, type }) => {
const clients = getClientsInRoom(roomId);
const time = Date.now();
clients.forEach(({ socketId }) => {
if (socketId !== socket.id)
io.to(socketId).emit(USER.MESSAGE, {
message,
username: usersMap[socket.id],
type,
time,
});
});
});
socket.on(USER.FOCUS_ON, ({ roomId }) => {
socket.in(roomId).emit(USER.FOCUS_ON, {
socketId: socket.id,
});
});
socket.on(USER.FOCUS_OFF, ({ roomId }) => {
socket.in(roomId).emit(USER.FOCUS_OFF, {
socketId: socket.id,
});
});
socket.on("disconnecting", () => {
const rooms = [...socket.rooms];
rooms.forEach((roomId) => {
socket.in(roomId).emit(USER.LEAVE, {
socketId: socket.id,
username: usersMap[socket.id],
});
});
delete usersMap[socket.id];
socket.leave();
});
});
const PORT = process.env.PORT || 5000;
server.listen(PORT, () => console.log(`Listening on port ${PORT}`));