-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTserver.js
executable file
·150 lines (125 loc) · 3.9 KB
/
Tserver.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
'use strict';
const Net = require('net');
const nextTick = require('./helpers/nextTick');
const Tsocket = require('./Tsocket');
const BaseAdapter = require('./adapters/BaseAdapter');
const { EventEmitter, on, once } = require('events');
const { v4 } = require('uuid');
const { Packr } = require('msgpackr');
const _ = require('lodash');
const packr = new Packr();
class Tserver extends EventEmitter {
static sockets = new Set();
constructor(port) {
super();
this.port = port;
this.id = v4();
this.server = new Net.Server();
this.middlewares = [];
this.adt = null;
this.setMaxListeners(0);
}
async *filterSockets(room) {
for (let value of this.constructor.sockets) {
await nextTick();
let finder = x => value.rooms.has(x);
if (_.isArray(room) && _.find(room, finder) && value?.connected === true) {
yield value;
continue;
}
if (value.rooms.has(room) && value?.connected === true) {
yield value;
}
}
}
async findSocket(room) {
for (let value of this.constructor.sockets) {
await nextTick();
let finder = x => value.rooms.has(x);
if (_.isArray(room) && _.find(room, finder) && value?.connected === true) {
return value;
}
if (value.rooms.has(room) && value?.connected === true) {
return value;
}
}
return null;
}
adapter(adt) {
if (adt instanceof BaseAdapter) {
this.adt = adt;
}
}
use(cb) {
if (_.isFunction(cb)) {
this.middlewares.push(cb);
}
}
to(room) {
return {
emit: this.emit.bind(this, room),
clients: this.emit.bind(this, room)
}
}
async emit(room, event, message, publish = true) {
if (!_.isString(event)) {
throw new Error('event name must be string');
}
if (publish && this.adt) {
let buff = packr.pack([this.id, room, event, message]);
this.adt.sendmsg(buff);
}
for await (let sock of this.filterSockets(room)) {
sock.emit(event, message);
}
}
async clients(room) {
if (this.adt) {
return await this.adt.clients(room);
}
let socks = new Set();
for await (let { id } of this.filterSockets(room)) {
socks.add(id);
}
return socks;
}
async connection(sock) {
let socket = new Tsocket(sock);
let nexter = middleware => (
resolve => {
let discard = () => resolve(false);
let id = setTimeout(discard, 60 * 1000);
let next = () => {
clearTimeout(id);
resolve(true);
};
middleware(socket, next);
}
);
for (let middleware of this.middlewares) {
let isNext = await new Promise(nexter(middleware));
if (isNext === false) {
return sock.destroy();
}
}
this.constructor.sockets.add(socket);
super.emit('connection', socket);
sock.on('close', () => this.constructor.sockets.delete(socket));
setImmediate(() => socket.emit('ready', true));
}
onAsync(event, signal = undefined) {
return on(this, event, { signal });
}
onceAsync(event, signal = undefined) {
return once(this, event, { signal });
}
async listen() {
if (_.isNil(this.adt) === false) {
this.adt.onmsg(this);
this.adt.onclients(this);
}
this.server.on('connection', this.connection.bind(this));
return new Promise(x => this.server.listen(this.port, () => x(this)));
}
}
module.exports = Tserver;