forked from Izumi-kun/dplayer-live-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
122 lines (103 loc) · 3.4 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const WebSocket = new require('ws');
const argv = require('minimist')(process.argv.slice(2), {string: ['port'], default: {port: 1207}});
const log4js = require('log4js');
log4js.configure({
appenders: {
info: {type: 'file', filename: 'info.log'},
error: {type: 'file', filename: 'error.log'},
},
categories: {
error: {appenders: ['error'], level: 'error'},
default: {appenders: ['info'], level: 'info'},
}
});
const errorLogger = log4js.getLogger('error');
const logger = log4js.getLogger();
let server = new WebSocket.Server({
clientTracking: true,
port: argv['port']
}, function () {
logger.debug('WebSocket server started on port: ' + argv['port']);
});
let shutdown = function () {
logger.debug('Received kill signal, shutting down gracefully.');
server.close(function () {
logger.debug('Closed out remaining connections.');
process.exit();
});
setTimeout(function () {
console.error('Could not close connections in time, forcefully shutting down');
process.exit();
}, 10 * 1000);
};
process.on('SIGTERM', shutdown);
process.on('SIGINT', shutdown);
server.on('error', function (err) {
errorLogger.error(err);
});
const typeRegExp = /^([012])$/;
let msgMinInterval = 500;
let lastMsgTimestamps = {};
let ipAddress = [];
server.on('connection', function (ws, req) {
let ip = req.headers['x-forwarded-for'] || req.connection.remoteAddress;
if (!ipAddress.includes(ip)) {
ipAddress.push(ip);
}
ws.on('message', function (message) {
if (message !== 'online') {
let time = Date.now();
if (lastMsgTimestamps[ip] && lastMsgTimestamps[ip] - time < msgMinInterval) {
return;
}
let msg;
try {
message = JSON.parse(message);
if (isNaN(message.color) || !typeRegExp.test(message.type) || !message.text) {
return;
}
msg = {
author: message.author ? message.author : '游客 Guest',
text: message.text,
color: message.color,
type: message.type
};
} catch (e) {
return;
}
lastMsgTimestamps[ip] = time;
let data = JSON.stringify(msg);
logger.info(ip + ' ' + data)
server.clients.forEach(function (client) {
if (client !== ws && client.readyState === WebSocket.OPEN) {
client.send(data, function (err) {
err && errorLogger.error(err);
});
}
});
} else {
if (!ipAddress.includes(ip)) {
ipAddress.push(ip);
}
}
});
ws.on('error', errorLogger.error);
});
setInterval(function () {
server.clients.forEach(function (client) {
if (client.readyState === WebSocket.OPEN) {
client.send(JSON.stringify({size: ipAddress.length}), function (err) {
err && errorLogger.error(err);
});
}
});
ipAddress = [];
}, 5000)
setInterval(function () {
let time = Date.now();
Object.keys(lastMsgTimestamps).forEach(function (key) {
if (time - lastMsgTimestamps[key] > msgMinInterval) {
delete lastMsgTimestamps[key];
}
});
}, 5000);