-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
35 lines (30 loc) · 732 Bytes
/
Copy pathindex.js
File metadata and controls
35 lines (30 loc) · 732 Bytes
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
const net = require("net");
const NetworkEvents = Object.freeze({
CONNECT: "connect",
TIMEOUT: "timeout",
ERROR: "error",
});
class NetworkTest extends net.Socket {
constructor(options, timeout) {
super(options);
super.setTimeout(timeout || 1000);
}
whenConnectTo(host, port) {
super.connect(port, host);
}
then(callback) {
this.on("connect", () => {
this.destroy();
callback(NetworkEvents.CONNECT, null);
});
this.on("timeout", () => {
this.destroy();
callback(NetworkEvents.TIMEOUT, null);
});
this.on("error", (error) => {
this.destroy();
callback(NetworkEvents.ERROR, error);
});
}
}
module.exports = { NetworkTest, NetworkEvents };