-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
36 lines (29 loc) · 1.04 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
const express = require('express');
const http = require('http');
const next = require('next');
const socketio = require('socket.io');
const port = parseInt(process.env.PORT || '3000', 10);
const dev = process.env.NODE_ENV !== 'production';
const nextApp = next({ dev });
const nextHandler = nextApp.getRequestHandler();
nextApp.prepare().then(async() => {
const app = express();
const server = http.createServer(app);
const io = new socketio.Server();
io.attach(server);
// app.get('/hello', async (_, res) => {
// res.send('Hello World')
// });
io.on('connection', (socket) => {
const count = io.engine.clientsCount;
console.log(socket.id)
console.log(`${count} players connected`)
socket.on('disconnect', () => {
console.log(`1 client disconnected : only ${count} players left`);
})
});
app.all('*', (req, res) => nextHandler(req, res));
server.listen(port, () => {
console.log(`--> NFighT is running on http://localhost:${port}`);
});
});