-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
72 lines (67 loc) · 2.07 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
'use strict';
const http = require('http');
const express = require('express');
const socketIO = require('socket.io');
const app = express();
const server = http.Server(app);
const io = socketIO(server);
app.use(express.static('static'));
const connections = new Map();
io.on('connection', socket => {
const whiteList = new Set(), blackList = new Set();
let currentName;
socket.on('rename', name => {
name = name.trim();
const oldName = currentName;
if(oldName !== undefined) {
if(connections.get(oldName) !== socket) {
socket.emit('rename-failed', oldName);
return;
}
connections.delete(oldName);
}
if(!name.length || name.charAt(0) === '-') {
socket.emit('rename-failed', oldName);
return;
}
connections.set(currentName = name, socket);
for(const others of connections.values())
others.emit('user-updated', { oldName, newName: currentName });
if(!oldName) socket.emit('online', Array.from(connections.keys()));
});
socket.on('sendmsg', (data) => {
const { to, message } = data;
to.split(/\s*,\s*/g).forEach(target => {
if(target.charAt(0) === '-')
blackList.add(target.substr(1));
else if(target.length)
whiteList.add(target);
});
if(whiteList.size && !blackList.size)
whiteList.add(currentName);
const msg = {
from: currentName,
message,
time: Date.now() / 1000,
group: (blackList.size || !whiteList.size) ? [] :
Array.from(whiteList.values()).sort()
};
connections.forEach((others, name) => {
if(name === currentName || (whiteList.size && !whiteList.has(name)) || blackList.has(name)) return;
others.emit('msgarrive', msg);
});
socket.emit('msgsent', msg);
whiteList.clear();
blackList.clear();
});
socket.on('disconnect', () => {
if(currentName) {
connections.delete(currentName);
for(const others of connections.values())
others.emit('user-updated', { oldName: currentName });
}
});
});
server.listen(8999, () => {
console.log('listening on *:8999');
});