-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
130 lines (95 loc) · 3.63 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
const express = require('express');
const app = express();
const http = require("http");
const path = require('path');
const { Server } = require('socket.io'); //Server class is being imported to create a new instance of the server
const ACTIONS = require('./src/Actions');
const server = http.createServer(app);
const io = new Server(server);
const userSocketMap = {};
function getConnectedUsers(roomID) {
const uniqueUsernames = new Set();
// Collect unique usernames
Array.from(io.sockets.adapter.rooms.get(roomID) || []).forEach(socketID => {
const username = userSocketMap[socketID];
if (username) {
uniqueUsernames.add(username);
}
});
// Convert set to array of objects with socketID and username
return Array.from(uniqueUsernames).map(username => {
const socketID = Object.keys(userSocketMap).find(id => userSocketMap[id] === username);
return { socketID, username };
});
}
io.on("connection", (socket) => {
console.log("Socket connected " + socket.id);
//listen to join event emitted by client
socket.on(ACTIONS.JOIN, ({roomID, username}) => {
userSocketMap[socket.id] = username;
socket.join(roomID);
const users = getConnectedUsers(roomID);
console.log(users);
// Emit JOINED event only to the user who just joined
socket.emit(ACTIONS.JOINED, {
users,
username,
socketID: socket.id,
});
// Emit updated user list to all users in the room
socket.broadcast.to(roomID).emit(ACTIONS.JOINED, {
users,
username,
socketID: socket.id,
});
});
socket.on(ACTIONS.CODE_CHANGE, ({roomID, code}) => { //listen to code change event emitted by client
socket.in(roomID).emit(ACTIONS.CODE_CHANGE,{code}); //send the code change event to all the users in the room
})
socket.on(ACTIONS.SYNC_CODE, ({code, socketID}) => { //same as above.
io.to(socketID).emit(ACTIONS.CODE_CHANGE, {code});
})
socket.on(ACTIONS.LANGUAGE_CHANGE, ({roomID, language}) => {
socket.in(roomID).emit(ACTIONS.LANGUAGE_CHANGE, {language});
})
socket.on(ACTIONS.OUTPUT_CHANGE, ({ output, roomID }) => {
socket.to(roomID).emit(ACTIONS.OUTPUT_CHANGE, { output });
});
socket.on('disconnect', () => {
const rooms = [...socket.rooms];
rooms.forEach((roomID) => {
socket.in(roomID).emit(ACTIONS.DISCONNECTED, {
socketID: socket.id,
username: userSocketMap[socket.id],
});
});
delete userSocketMap[socket.id]; //delete the disconnected user's entry from the userSocketMap
socket.leave();
})
})
require('dotenv').config();
const port = process.env.PORT;
/*
//-----------------------------DEPLOYMENT---------------------------------------
app.use(express.static(path.join(__dirname, 'build')));
app.get('*', (req, res) => {
res.sendFile(path.join(__dirname, 'build', 'index.html'));
});
const __dirname1 = path.resolve();
if (process.env.NODE_ENV === "production") {
app.use(express.static(path.join(__dirname1, "build")));
app.get("*", (req, res) =>
res.sendFile(path.resolve(__dirname1, "build", "index.html"))
);
} else {
app.get("/", (req, res) => {
res.send("API is running..");
});
}
console.log(__dirname);
console.log(path.join(__dirname, 'build'));
//----------------------------------------------------------------------
*/
server.listen(port, () =>{
console.log(`Server is running on port ${port}`);
})