This repository was archived by the owner on Sep 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.js
More file actions
75 lines (70 loc) · 1.94 KB
/
Copy pathclient.js
File metadata and controls
75 lines (70 loc) · 1.94 KB
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
const Logger = require("./logger");
const {createAddress} = require("./utils");
let client_list = []
class Client {
constructor(socket, type) {
this.socket = socket
this.type = type
this.address = createAddress(this.socket)
this.step = true // true / false
this.team = undefined
client_list.push(this)
}
disconnect() {
for (let i = 0; i < client_list.length; i++) {
let client = client_list[i]
if (client && client.address === this.address) {
if (client.socket && !client.socket.destroyed)
try {
client.socket.write(JSON.stringify({"command": "disconnect"}))
} catch (exception) {
Logger.warning(`Клиент ${this.address} отключен произвольно!`)
}
client_list.splice(i, 1);
}
}
}
send_data(data) {
if (this.socket) {
this.socket.write(JSON.stringify(data), 'utf-8');
return true
}
return false
}
broadcast_data(who, data) {
let k = 0
for (let client of getWithType(who)) {
if (client.socket) {
k += 1
client.send_data(data)
}
}
return k > 0
}
}
function getWithType(type) {
let array = []
for (let client of client_list) {
if (client.type === type)
array.push(client)
}
return array;
}
function getClient(address) {
for (let client of client_list) {
if (client.address === address)
return client;
}
return undefined;
}
// function countClientType(type) {
// let amount = 0
// for (let client of client_list) {
// if (client.type === type)
// amount += 1
// }
// return amount;
// }
module.exports = {
Client, client_list, getClient, getWithType
}