-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
146 lines (132 loc) · 4.02 KB
/
server.js
File metadata and controls
146 lines (132 loc) · 4.02 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
144
145
146
const express = require("express");
const app = express();
const http = require("http");
const path = require("path");
const { Server } = require("socket.io");
const bodyParser = require("body-parser");
const ACTIONS = require("./src/Actions");
const cors = require("cors");
var compiler = require("compilex");
var options = { stats: true }; //prints stats on console
compiler.init(options);
const server = http.createServer(app);
const io = new Server(server);
app.use(bodyParser.json());
app.use(cors());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("build"));
// app.use((req, res, next) => {
// res.sendFile(path.join(__dirname, "build", "index.html"));
// });
const userSocketMap = {};
function getAllConnectedClients(roomId) {
// Map
return Array.from(io.sockets.adapter.rooms.get(roomId) || []).map(
(socketId) => {
return {
socketId,
username: userSocketMap[socketId],
};
}
);
}
io.on("connection", (socket) => {
console.log("socket connected", socket.id);
socket.on(ACTIONS.JOIN, ({ roomId, username }) => {
userSocketMap[socket.id] = username;
socket.join(roomId);
const clients = getAllConnectedClients(roomId);
clients.forEach(({ 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, ({ socketId, code }) => {
io.to(socketId).emit(ACTIONS.CODE_CHANGE, { code });
});
socket.on("disconnecting", () => {
const rooms = [...socket.rooms];
rooms.forEach((roomId) => {
socket.in(roomId).emit(ACTIONS.DISCONNECTED, {
socketId: socket.id,
username: userSocketMap[socket.id],
});
});
compiler.flush(function () {
console.log("deleted temp files");
});
delete userSocketMap[socket.id];
socket.leave();
});
});
app.post("/compile", function (req, res) {
try {
var code = req.body.code;
var input = req.body.input;
var lang = req.body.lang;
if (lang == "python") {
if (input.length > 0) {
var envData = { OS: "windows", options: { timeout: 10000 } };
compiler.compilePythonWithInput(envData, code, input, function (data) {
return res.send(data);
});
} else {
var envData = { OS: "windows", options: { timeout: 10000 } };
compiler.compilePython(envData, code, function (data) {
return res.send(data);
});
}
} else if (lang == "cpp") {
if (input.length > 0) {
var envData = {
OS: "windows",
cmd: "g++",
options: { timeout: 10000 },
}; // (uses g++ command to compile )
//else
compiler.compileCPPWithInput(envData, code, input, function (data) {
return res.send(data);
});
} else {
var envData = {
OS: "windows",
cmd: "g++",
options: { timeout: 10000 },
}; // (uses g++ command to compile )
//else
compiler.compileCPP(envData, code, function (data) {
return res.send(data);
//data.error = error message
//data.output = output value
});
}
} else if (lang == "java") {
if (input.length > 0) {
var envData = { OS: "windows", options: { timeout: 10000 } };
//else
compiler.compileJavaWithInput(envData, code, input, function (data) {
return res.send(data);
});
} else {
//if windows
var envData = { OS: "windows", options: { timeout: 10000 } };
//else
compiler.compileJava(envData, code, function (data) {
return res.send(data);
});
}
}
} catch (error) {
return res.status(400).send(error);
}
//data.error = error message
//data.output = output value
});
const PORT = process.env.PORT || 5000;
server.listen(PORT, () => console.log(`Listening on port ${PORT}`));