-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
48 lines (40 loc) · 1.17 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
const log = require('electron-log');
const http = require('http');
const game = require('./game/game.js')
const startServer = () => {
const hostname = '127.0.0.1';
const port = 29301;
const server = http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/html'}); // http header
const url = req.url;
const method = req.method
if (url === '/' && method === 'POST') {
const body = []
req.on('data', chunk => {
body.push(chunk)
});
req.on('end', () => {
const jsonRespString = body.join('')
try {
const gameEvent = JSON.parse(jsonRespString)
game.onNewGameEvent(gameEvent)
game.checkForGameRunning()
} catch (error) {
log.error("Error parsing game event", error)
log.info("Data trying to parse", jsonRespString)
}
})
res.write("Success")
res.end()
} else {
res.write("Not supported")
res.end()
}
});
server.listen(port, hostname, () => {
log.info(`Listening on http://${hostname}:${port}/`);
});
}
module.exports = {
startServer
}