-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsocket-server.js
More file actions
103 lines (87 loc) · 3.73 KB
/
socket-server.js
File metadata and controls
103 lines (87 loc) · 3.73 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
'use strict';
const exec = require('child_process').exec;
const fs = require('fs');
var socketIO = require('socket.io');
var ot = require('ot');
var roomList = {};
module.exports = function (server) {
var str = 'console.log("InterCode is working");';
var io = socketIO(server);
io.on('connection', function (socket) {
socket.on('joinRoom', function (data) {
if (!roomList[data.room]) {
var socketIOServer = new ot.EditorSocketIOServer(str, [], data.room, function (socket, cb) {
// Fetching the code editor data
var self = this;
Task.findByIdAndUpdate(data.room, {content: self.document}, function (err) {
if (err) return cb(false);
cb(true);
});
});
roomList[data.room] = socketIOServer;
}
roomList[data.room].addClient(socket);
roomList[data.room].setName(socket, data.username);
socket.room = data.room;
socket.join(data.room);
});
socket.on('chatMessage', function (data) {
console.log("This chat data is send: " + data);
io.to(socket.room).emit('chatMessage', data);
});
// Receive the editor message from task.hbs and save the code in file
socket.on('editorMessage', function (data) {
if(data.lang === "nodeJS") {
// Save the code in a js file
fs.writeFile("./test.js", data.message, function (err) {
if (err) {
return console.log(err);
}
console.log("The file was saved!");
});
// Now execute the saved file
const child = exec('node test.js', (error, stdout, stderr) => {
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
if (error !== null) {
console.log(`exec error: ${error}`);
}
// Send back the compile output to task.hbs page
io.to(socket.room).emit('editorMessage', stdout);
console.log("// Send back the compile output to task.hbs page");
});
}
else {
// Save the code in a cpp file
fs.writeFile("./test.cpp", data.message, function (err) {
if (err) {
return console.log(err);
}
console.log("The file was saved!");
});
// Now execute the saved file
const child = exec('g++ test.cpp', (error, stdout, stderr) => {
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
if (error !== null) {
console.log(`exec error: ${error}`);
}
});
// Now execute the saved file
const child1 = exec('a.exe', (error, stdout, stderr) => {
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
if (error !== null) {
console.log(`exec error: ${error}`);
}
// Send back the compile output to task.hbs page
io.to(socket.room).emit('editorMessage', stdout);
console.log("// Send back the compile output to task.hbs page");
});
}
});
socket.on('disconnect', function () {
socket.leave(socket.room);
});
})
}