forked from Novage/wt-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimultaneous-connections.ts
More file actions
124 lines (105 loc) · 3.88 KB
/
Copy pathsimultaneous-connections.ts
File metadata and controls
124 lines (105 loc) · 3.88 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
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
/**
* Copyright 2019 Novage LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as WebSocket from "ws";
// tslint:disable:no-console
const peersCount = 10000;
const swarmsCount = 10;
const offersCount = 10;
const offers = new Array<any>();
for (let o = 0; o < offersCount; o++) {
offers.push({
offer: {
sdp: "asdfasdfasdfasdfasdfasdfasdf",
value: 1,
},
offer_id: "taasdfasdfasdfasd",
});
}
const closePromises = new Array<Promise<void>>();
const connectPromises = new Array<Promise<void>>();
console.log("creating", peersCount, "connections");
async function timeout(milliseconds: number) {
return new Promise<void>(resolve => setTimeout(resolve, milliseconds));
}
// tslint:disable-next-line:cognitive-complexity
async function main() {
try {
for (let p = 0; p < peersCount; p++) {
const webSocket = new WebSocket("ws://localhost:8000/");
closePromises.push(new Promise((resolve, reject) => {
webSocket.on("close", resolve);
webSocket.on("error", e => reject(new Error(`Socket closed ${e}`)));
}));
connectPromises.push(new Promise((resolve, reject) => {
webSocket.on("open", () => {
try {
webSocket.send(JSON.stringify({
action: "announce",
event: "started",
info_hash: Math.floor(swarmsCount * Math.random()).toPrecision(19).toString(),
peer_id: p.toPrecision(19).toString(),
numwant: offersCount,
offers: offers,
}));
resolve();
} catch (e) {
reject(new Error(`Send error ${e}`));
}
});
webSocket.on("message", message => {
const json = JSON.parse(message as string);
if (!json.offer) {
return;
}
try {
webSocket.send(JSON.stringify({
action: "announce",
info_hash: json.info_hash,
peer_id: p.toPrecision(19).toString(),
to_peer_id: json.peer_id,
answer: {
type: "answer",
sdp: "xxxxxxxxxxxx",
},
}));
} catch (e) {
reject(new Error(`Send error ${e}`));
}
});
webSocket.on("close", () => reject(new Error("Socket closed")));
webSocket.on("error", e => reject(new Error(`Socket closed ${e}`)));
}));
await timeout(10);
}
} catch (e) {
console.log("faied to create WebSocket connections", e);
return;
}
try {
await Promise.all(connectPromises);
} catch (e) {
console.log("socket error:", e);
return;
}
console.log("waiting for the connections to close");
try {
await Promise.all(closePromises);
} catch (e) {
console.log("socket error:", e);
}
console.log("done");
}
main();