-
-
Notifications
You must be signed in to change notification settings - Fork 248
/
Copy pathtcp_client_abort_signal.js
58 lines (50 loc) · 1.36 KB
/
tcp_client_abort_signal.js
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
"use strict";
// create an empty modbus client
// let ModbusRTU = require("modbus-serial");
const ModbusRTU = require("../index");
const client = new ModbusRTU();
const abortController = new AbortController();
const { signal } = abortController;
signal.addEventListener("abort", () => {
console.log("Abort signal received by the abort controller");
});
async function connect() {
await client.connectTCP("127.0.0.1", {
port: 8502,
socketOpts: {
signal: signal
}
});
client.setID(1);
client.setTimeout(2000);
}
async function readRegisters() {
const data = await client.readHoldingRegisters(5, 4);
console.log("Received:", data.data);
}
async function runner() {
await connect();
setTimeout(() => {
abortController.abort("Aborting request");
}, 1000);
await readRegisters();
}
runner()
.then(() => {
if (signal.aborted) {
if (signal.reason) {
console.log(`Request aborted with reason: ${signal.reason}`);
} else {
console.log("Request aborted but no reason was given.");
}
} else {
console.log("Request not aborted");
}
})
.catch((error) => {
console.error(error);
})
.finally(async() => {
console.log("Close client");
await client.close();
});