-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
59 lines (51 loc) · 1.67 KB
/
server.js
File metadata and controls
59 lines (51 loc) · 1.67 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
import { Server } from 'node:http';
import { parse } from 'node:url';
import next from 'next';
import { setHttpServer, setWebSocketServer } from 'next-ws/server';
import { WebSocketServer } from 'ws';
import * as esbuild from 'esbuild';
import './game/headless.js';
esbuild.context({
entryPoints: ['./public/client/main.js'],
bundle: true,
outfile: './public/game.bundle.js',
minify: true,
sourcemap: true
}).then((context) => {
console.log('Bundling game...');
return context.watch().then(() => {
console.log('Game bundle ready.');
});
});
//import './game/index.js'
const httpServer = new Server();
setHttpServer(httpServer);
const webSocketServer = new WebSocketServer({ noServer: true });
setWebSocketServer(webSocketServer);
const dev = process.env.NODE_ENV !== 'production';
const hostname = 'localhost';
const port = Number.parseInt(process.env.PORT ?? '9000');
const app = next({ dev, hostname, port, customServer: true });
const handle = app.getRequestHandler();
import { exec } from 'node:child_process';
exec('pkill -o chromium');
app.prepare().then(() => {
httpServer
.on('request', async (req, res) => {
const parsedUrl = parse(req.url, true);
res.setHeader('fly-region', process.env.FLY_REGION || 'offsite');
await handle(req, res, parsedUrl);
}).on('upgrade', (req, socket, head) => {
console.log(req.url);
if (req.url === '/_next/webpack-hmr')
socket.destroy();
})
.listen(port, () => {
console.log(` ▲ Ready on http://${hostname}:${port}`);
/*setInterval(() => {
fetch('http://localhost:9000/api/lobby/empty', {
method: 'POST'
});
}, 10000);*/
});
});