-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
88 lines (73 loc) · 2.5 KB
/
Copy pathserver.js
File metadata and controls
88 lines (73 loc) · 2.5 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
const express = require("express");
const app = express();
const http = require("http");
const { Server } = require("socket.io");
const ACTIONS = require("./src/Actions");
const path = require("path");
const httpserver = http.createServer(app);
const io = new Server(httpserver);
app.use(express.static("build"));
app.use((req, res, next) => {
res.sendFile(path.join(__dirname, "build", "index.html"));
});
const PORT = process.env.PORT || 3001;
httpserver.listen(PORT, () =>
console.log(`Server has started at Port : ${PORT}`)
);
const userSocketMap = {};
function getAllConnectedClients(roomId) {
return Array.from(
io.sockets.adapter.rooms.get(roomId) || []
).map((socketId) => {
return {
socketId,
username: userSocketMap[socketId],
};
});
}
io.on("connection", (socket) => {
console.log("socket connected with ID : ", socket.id);
// as soon as a client joins, this event is triggered by frontend
socket.on(ACTIONS.JOIN, ({ roomId, username }) => {
// store that username inside userSocketMap
userSocketMap[socket.id] = username;
// socketId/user joins a room with unique roomId
socket.join(roomId);
// get all clients in that roomId to send them notification of a new member
const clients = getAllConnectedClients(roomId);
// send message to current members in the room
clients.forEach(({ socketId }) => {
io.to(socketId).emit(ACTIONS.JOINED, {
clients,
// who is joining
username,
socketId: socket.id,
});
});
});
// code change listener
socket.on(ACTIONS.CODE_CHANGE, ({ roomId, code }) => {
// server also sends code_change event with updated code to all the users in roomId
socket.in(roomId).emit(ACTIONS.CODE_CHANGE, { code });
});
// sync code for initial join
socket.on(ACTIONS.SYNC_CODE, ({ socketId, code }) => {
// server also sends code_change event with updated code to all the users in roomId
io.to(socketId).emit(ACTIONS.CODE_CHANGE, { code });
});
// just before disconnecting
socket.on("disconnecting", () => {
// get all the rooms
const rooms = [...socket.rooms];
// broadcast message in every room of socket.id, that user with given socket.id is going to disconnect
rooms.forEach((roomId) => {
socket.in(roomId).emit(ACTIONS.DISCONNECTED, {
socketId: socket.id,
username: userSocketMap[socket.id],
});
});
// delete the socket.id from userSocketMap
delete userSocketMap[socket.id];
socket.leave();
});
});