-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathconnection.ts
More file actions
59 lines (49 loc) · 1.46 KB
/
connection.ts
File metadata and controls
59 lines (49 loc) · 1.46 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
/*
Use this script to test grid connection:
This script creates 2 instances of the grid client, then after 10 minutes, it performs a query on both instances.
Steps:
1 - Connect to the grid
2 - Close the internet connection
3 - After 9 minutes, open the internet connection again
The grid should automatically reconnect and request the twin ID.
*/
import { GridClient, NetworkEnv } from "../src";
// Replace with actual mnemonics
const clientMnemonic1 = "your_mnemonic_here";
const clientMnemonic2 = "your_mnemonic_here";
// Choose the desired network (main, test, qa, dev)
const NETWORK = NetworkEnv.dev;
// Client 1 logic
async function newClient(mnemonic: string) {
const options = {
network: NETWORK,
mnemonic: mnemonic,
};
try {
const gridClient = new GridClient(options);
await gridClient.connect();
setTimeout(
async () => {
console.log(`Request the grid to get the twin id.`);
const twinID = await gridClient.twins.get_my_twin_id();
console.log(`Twin id: ${twinID}`);
},
10 * 60 * 1000,
);
} catch (error) {
console.error("Error connecting to Grid:", error);
}
}
// Main function
async function main() {
await newClient(clientMnemonic1);
await newClient(clientMnemonic2);
console.log(`Please disconnect your internet connection.`);
setTimeout(
async () => {
console.log(`Please re-connect your internet connection.`);
},
9 * 60 * 1000,
);
}
main();