This repository has been archived by the owner on Jul 1, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.ts
104 lines (94 loc) · 2.4 KB
/
test.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import * as net from "net";
//let s: net.Socket = net.createConnection("/srv/local/dev.apis.cfx.private/app.sock");
let s: net.Socket = net.createConnection("/tmp/app.sock");
let reqs: string[] = [
"GET\u001e/brokerage/v2/assets/SCI\0",
"GET\u001e/brokerage/v2/assets/BAQUA\0",
"GET\u001e/brokerage/v2/assets/BIOLIFE4D\0",
"GET\u001e/brokerage/v2/assets/INVT001\0",
"GET\u001e/brokerage/v2/assets/BCAP\0",
];
let currentRequest: number = 0;
let outstandingCalls: number = reqs.length;
let readCount = 0;
s.on("data", function(data:string) {
data = data.toString();
if (data.substring(data.length - 1) === "\0") {
data = data.substring(0, data.length - 1);
}
console.log(++readCount, data);
outstandingCalls--;
/*
let decrement: boolean = false;
if (data.substring(data.length - 1) === "\0") {
data = data.substring(0, data.length - 1);
decrement = true;
}
let spl: string[] = data.split("\n");
console.log(++readCount, spl[0], JSON.stringify(JSON.parse(spl[1]), null, 2));
if (decrement) {
outstandingCalls--;
}
*/
});
s.on("error", function(err: Error) {
console.log("SOCKET ERROR: ", err);
});
s.on("close", function(hadError: boolean) {
if (hadError) {
console.log("Closed with error");
} else {
console.log("Closed without error");
}
});
s.on("connect", function() {
console.log("Connected");
});
s.on("drain", function() {
console.log("buffer emptied");
write();
});
s.on("end", function() {
console.log("ended");
});
s.on("lookup", function() {
console.log("hostname resolved");
});
s.on("timeout", function() {
console.log("Timed out");
});
s.on("ready", function() {
console.log("Ready for writing");
});
/*
let j: any = setInterval(function() {
if (currentRequest >= reqs.length) {
clearInterval(j);
return;
}
if (paused) {
return;
}
if (s.write(reqs[currentRequest])) {
console.log("Wrote " + reqs[currentRequest] + " (" + s.bytesWritten + " bytes written)");
currentRequest++;
} else {
paused = true;
}
}, 1);
*/
function write() {
while(currentRequest < reqs.length && s.write(reqs[currentRequest])) {
console.log("Wrote " + reqs[currentRequest] + " (" + s.bytesWritten + " bytes written)");
currentRequest++;
}
}
write();
// Make sure to clean up
let i: any = setInterval(function() {
if (outstandingCalls < 1) {
console.log("Destroying");
s.destroy();
clearInterval(i);
}
}, 50);