-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
142 lines (116 loc) · 4.07 KB
/
server.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
const app = require("express")();
const http = require("http");
const { Server } = require("socket.io");
const cors = require("cors");
app.use(cors());
const server = http.createServer(app);
const io = new Server(server, {
cors: {
origin: "*",
methods: ["GET", "POST"],
},
});
app.get("/", function (req, res) {
res.send("Hello from the server!");
});
const socketID_to_Users_Map = {};
const roomID_to_Code_Map = {};
async function getUsersinRoom(roomId, io) {
const socketList = await io.in(roomId).allSockets();
const userslist = [];
socketList.forEach((each) => {
each in socketID_to_Users_Map &&
userslist.push(socketID_to_Users_Map[each].username);
});
return userslist;
}
async function updateUserslistAndCodeMap(io, socket, roomId) {
socket.in(roomId).emit("member left", {
username: socketID_to_Users_Map[socket.id].username,
});
// update the user list
delete socketID_to_Users_Map[socket.id];
const userslist = await getUsersinRoom(roomId, io);
socket.in(roomId).emit("updating client list", { userslist: userslist });
userslist.length === 0 && delete roomID_to_Code_Map[roomId];
}
//Whenever someone connects this gets executed
io.on("connection", function (socket) {
console.log("A user connected", socket.id);
socket.on("when a user joins", async ({ roomId, username }) => {
console.log("username: ", username);
socketID_to_Users_Map[socket.id] = { username };
socket.join(roomId);
const userslist = await getUsersinRoom(roomId, io);
// for other users, updating the client list
socket.in(roomId).emit("updating client list", { userslist: userslist });
// for this user, updating the client list
io.to(socket.id).emit("updating client list", { userslist: userslist });
// send the latest code changes to this user when joined to existing room
if (roomId in roomID_to_Code_Map) {
io.to(socket.id).emit("on language change", {
languageUsed: roomID_to_Code_Map[roomId].languageUsed,
});
io.to(socket.id).emit("on code change", {
code: roomID_to_Code_Map[roomId].code,
});
}
// alerting other users in room that new user joined
socket.in(roomId).emit("new member joined", {
username,
});
});
// for other users in room to view the changes
socket.on("update language", ({ roomId, languageUsed }) => {
if (roomId in roomID_to_Code_Map) {
roomID_to_Code_Map[roomId]["languageUsed"] = languageUsed;
} else {
roomID_to_Code_Map[roomId] = { languageUsed };
}
});
// for user editing the code to reflect on his/her screen
socket.on("syncing the language", ({ roomId }) => {
if (roomId in roomID_to_Code_Map) {
socket.in(roomId).emit("on language change", {
languageUsed: roomID_to_Code_Map[roomId].languageUsed,
});
}
});
// for other users in room to view the changes
socket.on("update code", ({ roomId, code }) => {
if (roomId in roomID_to_Code_Map) {
roomID_to_Code_Map[roomId]["code"] = code;
} else {
roomID_to_Code_Map[roomId] = { code };
}
});
// for user editing the code to reflect on his/her screen
socket.on("syncing the code", ({ roomId }) => {
if (roomId in roomID_to_Code_Map) {
socket
.in(roomId)
.emit("on code change", { code: roomID_to_Code_Map[roomId].code });
}
});
socket.on("leave room", ({ roomId }) => {
socket.leave(roomId);
updateUserslistAndCodeMap(io, socket, roomId);
});
socket.on("disconnecting", (reason) => {
socket.rooms.forEach((eachRoom) => {
if (eachRoom in roomID_to_Code_Map) {
updateUserslistAndCodeMap(io, socket, eachRoom);
}
});
});
//Whenever someone disconnects this piece of code executed
socket.on("disconnect", function () {
console.log("A user disconnected");
});
});
//you can store your port number in a dotenv file, fetch it from there and store it in PORT
//we have hard coded the port number here just for convenience
const PORT = process.env.PORT || 5000;
server.listen(PORT, function () {
console.log(`listening on port : ${PORT}`);
});