-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTsocket.js
executable file
·89 lines (74 loc) · 2.33 KB
/
Tsocket.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
'use strict';
const { Packr, UnpackrStream } = require('msgpackr');
const { EventEmitter, on, once } = require('events');
const { v4 } = require('uuid');
const sleep = require('./helpers/sleep');
const _ = require('lodash');
const packr = new Packr();
class Tsocket extends EventEmitter {
constructor(sock) {
super();
let close = () => {
super.emit('disconnect', this);
this.connected = false;
};
this.connected = true;
this.sock = sock;
this.id = v4();
this.rooms = new Set([this.id]);
this.sock.on('close', close);
this.sock.on('error', e => super.emit('error', e));
this.sock.setKeepAlive(true, 5000);
this.setMaxListeners(0);
this.bindOnData();
}
async bindOnData() {
try {
let stream = new UnpackrStream({ mapsAsObjects: true });
this.sock.pipe(stream);
for await (let chunk of stream) {
let [event, vars, id] = chunk;
if (_.isString(event) && _.isNil(id) === false) {
super.emit(event, vars, this.emit.bind(this, `${event}:${id}`));
}
}
} catch (error) {
}
}
join(room) {
this.rooms.add(room);
return this;
}
leave(room) {
this.rooms.delete(room);
return this;
}
onAsync(event, signal = undefined) {
return on(this, event, { signal });
}
onceAsync(event, signal = undefined) {
return once(this, event, { signal });
}
async emit(event, vars, wait = false) {
let id = v4();
for (let i = 0; i < 10; i++) {
if (this.connected === false) {
await sleep(1000);
continue;
}
break;
}
if (this.connected === false) {
return false;
}
if (wait === true) {
let ac = new AbortController();
let timeout = setTimeout(() => ac.abort(), 10 * 1000);
let res = this.onceAsync(`${event}:${id}`, ac.signal);
this.sock.write(packr.pack([event, vars, id]));
return res.then(x => _.first(x)).finally(() => clearTimeout(timeout));
}
this.sock.write(packr.pack([event, vars, id]));
}
}
module.exports = Tsocket;