-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
206 lines (150 loc) · 4.48 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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// server.js
const express = require('express');
const http = require('http');
const path = require('path');
const { Server } = require('socket.io');
const bodyParser = require('body-parser');
const cors = require('cors');
const compiler = require('compilex');
const ACTIONS = require('./src/Actions');
const app = express();
const server = http.createServer(app);
const io = new Server(server);
const option = { stats: true };
compiler.init(option);
app.use(cors());
app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.get('/', function (req, res) {
compiler.flush(function(){
console.log("temporary executed file deleted")
})
res.sendFile(__dirname + "/index.html");
});
app.post('/compile', function (req, res) {
var code = req.body.code;
var input = req.body.input;
var lang = req.body.lang;
try {
if (lang == 'C++') {
var envData = { OS: "windows", cmd: "g++", options: { timeout: 10000 } };
if (!input) {
compiler.compileCPP(envData, code, function (data) {
if (data.output) {
res.send(data);
} else {
res.send({ output: 'error' });
}
});
} else {
compiler.compileCPPWithInput(envData, code, input, function (data) {
if (data.output) {
res.send(data);
} else {
res.send({ output: 'error' });
}
});
}
} else if (lang == 'java') {
// Similar logic for Java compilation...
if(!input){
var envData = { OS : "windows"};
//else
var envData = { OS : "linux" }; // (Support for Linux in Next version)
compiler.compileJava( envData , code , function(data){
if(data.output){
res.send(data);
}
else{
res.send({output:'error'})
}
});
}else
{
var envData = { OS : "windows"};
compiler.compileJavaWithInput( envData , code , input , function(data){
if(data.output){
res.send(data);
}
else{
res.send({output:'error'})
}
});
}
} else if (lang == 'python') {
// Similar logic for Python compilation...
if(!input){
var envData = { OS : "windows"};
compiler.compilePython( envData , code , function(data){
if(data.output){
res.send(data);
}
else{
res.send({output:'error'})
}
});
}
else
{
var envData = { OS : "windows"};
compiler.compilePythonWithInput( envData , code , input , function(data){
if(data.output){
res.send(data);
}
else{
res.send({output:'error'})
}
});
}
}
} catch (e) {
console.log(e);
}
});
const userSocketMap = {};
io.on('connection', (socket) => {
console.log('socket connected', socket.id);
socket.on('user_send_message', ({ roomId, username, message }) => {
socket.to(roomId).emit('user_message',({message,username}))
})
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],
});
});
delete userSocketMap[socket.id];
socket.leave();
});
});
function getAllConnectedClients(roomId) {
return Array.from(io.sockets.adapter.rooms.get(roomId) || []).map(
(socketId) => {
return {
socketId,
username: userSocketMap[socketId],
};
}
);
}
server.listen(5000, () => console.log('Server is listening on port 5000'));