-
Notifications
You must be signed in to change notification settings - Fork 0
/
web-server.js
67 lines (48 loc) · 1.48 KB
/
web-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
const
spawn = require('child_process').spawn,
express = require('express'),
app = express(),
http = require('http').Server(app),
io = require('socket.io')(http);
var mcServerProcess, loadingCounter;
app.use(express.static(__dirname + '/dist'));
app.get('/',(req, res)=>{
res.sendFile(__dirname + '/dist/index.html');
});
io.on('connection',(socket)=>{
// After connecting the socket
socket.emit('connectApp', mcServerProcess ? 'online' : 'offline');
socket.on('start-server',()=>{
runMcServer(socket);
})
socket.on('stop-server',()=>{
stopMcServer(socket);
});
socket.on('disconnect',()=>{
console.log('user disconnected');
})
});
http.listen(3030,()=>{
console.log('web server listening on *:3030');
});
function runMcServer(socket){
mcServerProcess = spawn('sh',['./mc/ServerStart.sh']);
socket.emit('serverIsLoading');
socket.broadcast.emit('serverIsLoading');
function log(data){
var row = data.toString();
process.stdout.write(row);
if(row.includes('[Minecraft] Done')){
//Server is online
socket.emit('serverIsLoaded');
socket.broadcast.emit('serverIsLoaded');
}
}
mcServerProcess.stdout.on('data', log);
mcServerProcess.stderr.on('data', log);
}
function stopMcServer(socket){
mcServerProcess.stdin.write('stop'+'\n');
socket.emit('stop-server');
socket.broadcast.emit('stop-server');
}