-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
60 lines (52 loc) · 1.61 KB
/
index.js
File metadata and controls
60 lines (52 loc) · 1.61 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
const express = require('express')
const cp = require('child_process')
const EventEmitter = require('events');
const socket = require('socket.io');
//App setup
const app = express();
const emitter = new EventEmitter();
const PORT = process.env.PORT || 5000;
const backend = app.listen(PORT,'0.0.0.0', () => console.log(`Server running on ${PORT}.
\nTo open the control panel, goto \x1b[36mhttp://localhost:${PORT}\x1b[0m in your browser.
\nThis control panel was made by \x1b[33mCodeWithTheDoctor\x1b[0m.`))
let server = undefined;
//Static files
app.use(express.static('public'))
//Socket setup
const io = socket(backend);
io.on('connection', socket=> {
console.log(`Socket connection established. ID: ${socket.id}`);
socket.on('start', ()=> {
//Spawn server child process
if(!server) {
console.log("Startin the servah");
io.sockets.emit("consoleMessage","Starting the Minecraft server...");
server = cp.spawn('java', ['-Xmx1024M', '-Xms1024M', '-jar', 'server.jar', 'nogui'], {
cwd: "server"
});
//Server child process stream listners
server.stdout.on('data', stdout=> {
console.log(stdout.toString());
io.sockets.emit("consoleMessage",stdout.toString());
})
server.stderr.on('data', stderr=> {
console.log(stderr.toString());
});
server.on('close', code=> {
console.log("lol, we've stopped :)")
})
}
else{
console.log("Server already running!");
}
});
socket.on('stop',()=> {
if(!server) {
console.log("Please start zi server first.")
} else {
server.stdin.write("stop");
server.stdin.end();
server = undefined;
}
});
})