-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
85 lines (78 loc) · 2.53 KB
/
index.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { availableParallelism } from 'node:os';
import cluster from 'node:cluster';
import { createServer } from 'node:http';
import { fileURLToPath } from 'node:url';
import { dirname, join } from 'node:path';
import express from 'express';
import { createAdapter, setupPrimary } from '@socket.io/cluster-adapter';
import { Server } from 'socket.io';
import createLogger from './src/logger.js';
import createConfig from './src/config/config-mgr.js';
import createDB from './src/db.js';
/**
* The logger instance
*
*/
const logger = createLogger('server');
/**
* Configuration instance
*
*/
const config = await createConfig();
/**
* Database connection instance
*
*/
const database = await createDB(config);
/**
* The main entry point of the application
* if the clusters are enabled, the primary process will fork the workers
* otherwise, the server will run in a single process
*/
if (cluster.isPrimary) {
const numCPUs = availableParallelism();
database.init();
let cpus = config.clusters ? numCPUs: 1;
for (let i = 0; i < cpus; i++) {
cluster.fork({
PORT: config.basePort + i
});
}
setupPrimary();
} else {
const app = express();
const server = createServer(app);
const io = new Server(server, {
connectionStateRecovery: {},
adapter: createAdapter()
});
const __dirname = dirname(fileURLToPath(import.meta.url));
app.get('/', (req, res) => {
res.sendFile(join(__dirname, 'html/index.html'));
});
io.on('connection', async (socket) => {
logger.debug('new user connected');
io.emit('connection', "new user connected");
socket.on('chat message', async (nickname, msg, clientOffset, callback) => {
logger.chat(nickname, msg);
const result = database.insertMessage(nickname, msg, clientOffset);
io.emit('chat message',nickname, msg, result.lastID);
callback();
socket.on('disconnect', () => {
io.emit('connection', "user disconnected");
logger.debug('user disconnected');
});
});
if (!socket.recovered) {
logger.debug('socket not recovered');
database.retreiveMesaages((nickname, msg, id) => {
socket.emit('chat message',nickname, msg, id);
});
}
});
server.listen(process.env.PORT, () => {
logger.highlight(`Running at http://localhost:${process.env.PORT}`);
}).on('error', (e) => {
logger.error('Error starting server', e);
});
}