-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
143 lines (118 loc) · 3.51 KB
/
Copy pathindex.js
File metadata and controls
143 lines (118 loc) · 3.51 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
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
143
/** @format */
import express from "express";
import http from "http";
import * as path from "path";
import mongoose from "mongoose";
import bodyParser from "body-parser";
import { createClient } from "redis";
import cors from "cors";
import { Server } from "socket.io";
import ACTIONS from "./Actions.js";
import codeHistoryRoute from "./routes/CodeHistory.js";
import codeRunRoute from "./routes/CodeRun.js";
const app = express();
const server = http.createServer(app);
import * as dotenv from "dotenv";
dotenv.config();
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
const io = new Server(server, {
cors: {
origins: ["*"],
methods: ["GET", "POST"],
transports: ["websocket", "polling"],
credentials: true,
},
allowEIO3: true,
});
//Routes
app.use("/codehistory", codeHistoryRoute);
app.use("/code", codeRunRoute);
// if (process.env.NODE_ENV == "production") {
app.use(express.static("./client/build"));
app.get("*", (req, res) => {
const dirname = path.resolve(path.dirname(""));
res.sendFile(path.resolve(dirname, "client", "build", "index.html"));
});
// }
const PORT = process.env.PORT || 5000;
//DB
mongoose
.connect(process.env.MONGODB_URL, {
useNewUrlParser: true,
useUnifiedTopology: true,
})
.then(() => {
console.log("DB Connection established");
});
const client = createClient({
url: `redis://default:${process.env.REDIS_PASSWORD}@${process.env.REDIS_URI}:${process.env.REDIS_PORT}`,
});
client.on("connect", () => console.log("Redis connected"));
client.on("error", (err) => console.log("Redis Connection Error", err));
client.connect();
const userSocketMap = {};
const getAllRedisClints = async (roomId) => {
const clients = await client.lRange(roomId, 0, -1);
const data = [];
clients.map((c) => data.push(JSON.parse(c)));
data.map((d) => {
userSocketMap[d.socketId] = d.userName;
});
return data;
};
io.on("connection", (socket) => {
console.log("Socket connected", socket.id);
socket.on(ACTIONS.JOIN, async ({ roomId, userName }) => {
// userSocketMap[socket.id] = userName;
const data = {
socketId: socket.id,
userName,
};
if (client.exists(roomId)) {
client.lPush(roomId, JSON.stringify(data));
} else {
client.lPush(roomId, JSON.stringify(data));
client.expire(roomId, 36000);
}
socket.join(roomId);
// const clients = getAllConnectedClients(roomId);
const clients = await getAllRedisClints(roomId);
clients?.map(({ socketId }) => {
io.to(socketId).emit(ACTIONS.JOINED, {
clients,
userName,
socketId: socket.id,
});
});
});
socket.on(ACTIONS.CODE_CHANGE, ({ roomId, code }) => {
socket.in(roomId).emit(ACTIONS.CODE_CHANGE, { code });
});
socket.on(ACTIONS.SYNC_CODE, ({ code, socketId }) => {
io.to(socketId).emit(ACTIONS.CODE_CHANGE, { code });
});
socket.on("disconnecting", () => {
const rooms = [...socket.rooms];
console.log(rooms);
rooms.forEach((roomId) => {
socket.in(roomId).emit(ACTIONS.DISCONNECTED, {
socketId: socket.id,
userName: userSocketMap[socket.id],
});
const data = {
socketId: socket.id,
userName: userSocketMap[socket.id],
};
client.LREM(roomId, 1, JSON.stringify(data));
});
delete userSocketMap[socket.id];
socket.leave();
});
});
server.listen(PORT, () => {
console.log("Sever up and Running at " + PORT);
});