-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.js
100 lines (88 loc) · 2.33 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
const WebSocket = require('ws');
const ABNORMAL = 1006;
const KA = 'KA';
class Room {
constructor() {
this._peers = new Map();
this._socks = new WeakMap();
}
get size() {
return this._peers.size;
}
hasPeer(peerId) {
return this._peers.has(peerId);
}
getPeerIdBySocket(socket) {
return this._socks.get(socket);
}
addPeer(socket, peerId) {
this._peers.set(peerId, socket);
this._socks.set(socket, peerId);
}
removePeer(socket) {
this._peers.delete(this._socks.get(socket));
}
broadcast(data) {
for (let socket of this._peers.values()) {
if (socket.readyState === WebSocket.OPEN) {
socket.send(data);
}
}
}
sendTo(peerId, data) {
const socket = this._peers.get(peerId);
if (socket) {
socket.send(data);
}
}
}
function getServer({ onMessage, ...config } = {}) {
const wss = new WebSocket.Server(config);
const roomBySocket = new WeakMap();
const rooms = {};
wss.on('connection', ws => {
const onClose = reason => {
const roomName = roomBySocket.get(ws);
if (roomName && rooms[roomName]) {
const room = rooms[roomName];
const peerId = room.getPeerIdBySocket(ws);
room.removePeer(ws);
if (reason === ABNORMAL) {
return setTimeout(() => {
if (!room.hasPeer(peerId)) {
onClose();
}
}, 500);
}
if (room.size === 0) {
delete rooms[roomName];
} else {
const message = {
room: roomName,
origin: peerId,
type: 'DOWN',
};
room.broadcast(JSON.stringify(message));
onMessage && onMessage(message);
}
}
};
ws.on('close', onClose);
ws.on('error', onClose);
ws.on('message', msg => {
const { room: roomName, origin, target, type, data } = JSON.parse(msg);
onMessage && onMessage({ room: roomName, origin, target, type, data });
if (!rooms[roomName]) rooms[roomName] = new Room();
const room = rooms[roomName];
if (!room.hasPeer(origin)) {
room.addPeer(ws, origin);
roomBySocket.set(ws, roomName);
}
if (type === KA) return;
if (!target) return room.broadcast(msg);
return room.sendTo(target, msg);
});
});
return wss;
}
module.exports = getServer;