-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
32 lines (29 loc) · 802 Bytes
/
server.js
File metadata and controls
32 lines (29 loc) · 802 Bytes
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
const port = 33850 // Your port here
const http = require('http');
const WebSocket = require('ws');
const rateLimit = require('ws-rate-limit')('5s', 10)
const server = http.createServer();
const wss = new WebSocket.Server({ server });
var ips = []
wss.on('connection', function connection(ws, req) {
rateLimit(ws);
const ip = req.socket.remoteAddress;
if (ips.includes(ip)) {
ws.close();
return;
} else {
ips.push(ip);
}
ws.on('message', function incoming(data) {
if (data.length > 1000) { return; }
wss.clients.forEach(function each(client) {
if (client !== ws && client.readyState === WebSocket.OPEN) {
client.send(data);
}
});
});
ws.on('close', function close() {
ips = ips.filter(item => item !== ip)
});
});
server.listen(port);