/
ConnectionManager.ts
89 lines (80 loc) · 2.93 KB
/
ConnectionManager.ts
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
import { WebSocket } from "ws";
import { Connection } from "./Connection";
import { ConnectionID } from "./ConnectionID";
export class ConnectionManager {
private connections : Connection[];
constructor() {
this.connections = [];
}
/**
* Получает сокет по ид для отправки пакета
* @param id string ид соединения
* @returns socket или null
*/
public getSocketByID(id : ConnectionID) : WebSocket {
for(let i = 0; i < this.connections.length; i++){
let connection : Connection = this.connections[i];
if(connection.id === id){
return connection.socket;
}
}
return null;
}
/**
* Добавляет соединение на сервер, генерирует ему ид
* @param socket WebSocket сокет
* @returns string ид соединения для дальнейших обращений
*/
public addConnection(socket : WebSocket) : ConnectionID {
let id = this.genID();
this.connections.push({socket: socket, id : id});
return id;
}
/**
* Удаляет соединение из таблицы соединений
* @param socket отключенный сокет
*/
public removeConnection(socket : WebSocket) : void {
for(let i = 0; i < this.connections.length; i++){
let connection : Connection = this.connections[i];
if(connection.socket === socket){
this.connections.splice(i, 1);
}
}
}
/**
* Удаляет соединение из таблицы соединений по ид
* @param id отключенный ид
*/
public removeConnectionByID(id : ConnectionID) : void {
for(let i = 0; i < this.connections.length; i++){
let connection : Connection = this.connections[i];
if(connection.id === id){
this.connections.splice(i, 1);
}
}
}
/**
* Получить ид соединения по сокету
* @param socket сокет
* @returns ид соединения
*/
public getConnectionBySock(socket : WebSocket) : ConnectionID {
for(let i = 0; i < this.connections.length; i++){
let connection : Connection = this.connections[i];
if(connection.socket === socket){
return connection.id;
}
}
}
private genID() : string {
let length = 6;
let result = '';
let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
let charactersLength = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * charactersLength));
}
return result;
}
}