-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.js
127 lines (93 loc) · 2.8 KB
/
client.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
import { v4 } from "uuid";
import { pack } from "msgpackr";
import { getSlaveData } from "./master.js";
import { counter, danger, muted, success, warning, info } from "./colors.js";
const connections = {},
total = {};
function increment(server, type) {
if (!(server in total)) {
total[server] = {};
}
if (!(type in total[server])) {
total[server][type] = 0;
}
total[server][type]++;
}
function decrement(server, type) {
if (server in total && type in total[server]) {
total[server][type]--;
}
if (total[server][type] <= 0) {
delete total[server][type];
}
if (Object.keys(total[server]).length === 0) {
delete total[server];
}
}
function count(server, type) {
if (server in total && type in total[server]) {
return total[server][type];
}
return 0;
}
function sendFullData(client, server, type) {
const data = getSlaveData(server, type);
if (!data) {
client.emit("no_data");
return;
}
client.emit("reset", Uint8Array.from(pack(data)).buffer);
}
export function handleConnection(client, server, type, license) {
const self = {
id: v4(),
client: client,
server: server,
type: type,
license: license,
paused: false,
};
increment(self.server, self.type);
connections[self.id] = self;
console.log(`${success("Connected")} ${muted(`{${self.id}}`)} ${info(`${self.server}/${self.type}`)} - ${counter(count(self.server, self.type))}`);
self.client.on("disconnect", () => {
decrement(self.server, self.type);
delete connections[self.id];
console.log(`${danger("Disconnected")} ${muted(`{${self.id}}`)} ${info(`${self.server}/${self.type}`)} - ${counter(count(self.server, self.type))}`);
});
self.client.on("pause", pPause => {
self.paused = pPause;
if (self.paused) {
console.log(`${warning("Paused")} ${muted(`{${self.id}}`)} ${info(`${self.server}/${self.type}`)}`);
} else {
console.log(`${success("Resumed")} ${muted(`{${self.id}}`)} ${info(`${self.server}/${self.type}`)}`);
sendFullData(self.client, self.server, self.type);
}
});
sendFullData(self.client, self.server, self.type);
}
export function getActiveViewers(server, type) {
const viewers = [];
for (const id in connections) {
if (!connections.hasOwnProperty(id)) continue;
const client = connections[id],
license = client.license;
if (client.type === type && client.server === server && !viewers.includes(license)) {
viewers.push(license);
}
}
return viewers;
}
export function handleDataUpdate(type, server, data) {
if (count(server, type) === 0 || Object.keys(data).length === 0) {
return;
}
data = pack(data);
for (const id in connections) {
if (!connections.hasOwnProperty(id)) continue;
const client = connections[id];
if (!client.paused && client.type === type && client.server === server) {
client.client.emit("message", Uint8Array.from(data).buffer);
}
}
}