Added a local test script, which will be supplemented after the diff is createdAdded a local test script.
```
// Alitatest.ts
import { ChronikClient, ConnectionStrategy } from './src/ChronikClient';
async function sleep(ms: number): Promise<void> {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function main() {
try {
const urls = [
'https://chronik.e.cash',
'https://chronik1.alitayin.com',
'https://chronik-native1.fabien.cash',
'https://chronik-native2.fabien.cash',
'https://chronik-native3.fabien.cash',
'https://chronik2.alitayin.com'
];
console.log('===== Starting WebSocket Reconnection Test =====');
const chronik = await ChronikClient.useStrategy(
ConnectionStrategy.ClosestFirst,
urls
);
const wsEndpoint = chronik.ws({
onMessage: (msg) => {
if (msg.type === 'Block') {
console.log(`Received block message: ${msg.blockHash}`);
} else if (msg.type === 'Tx') {
console.log(`Received transaction message: ${msg.txid}`);
} else if (msg.type === 'Error') {
console.log(`Received error message: ${msg.msg}`);
}
},
onConnect: () => console.log('WebSocket connected'),
onReconnect: () => console.log('WebSocket reconnecting'),
onError: () => console.log('WebSocket connection error'),
onEnd: () => console.log('WebSocket connection ended'),
});
console.log('Connecting to WebSocket...');
await wsEndpoint.waitForOpen();
console.log('Subscribing to block updates...');
wsEndpoint.subscribeToBlocks();
await sleep(60000);
wsEndpoint.close();
console.log('===== WebSocket Reconnection Test Completed =====');
} catch (error) {
console.error('Test error:', error);
return 1;
}
return 0;
}
main().then(process.exit);
```
When directly testing this script, the nodes connect normally without any failures. We need to simulate nodes that are normally reachable but immediately disconnect, so we need to add the following at the end of `wsEndpoint.connected = new Promise` (line 333):
```
console.log(`Node ${this._workingIndex} connected, closing immediately to test reconnection mechanism`);
setTimeout(() => ws.close(), 100);
```
This allows us to simulate nodes that establish a WebSocket connection but actually fail right after. Then, when we run the script, we get:
```
===== Starting WebSocket Reconnection Test =====
1. https://chronik2.alitayin.com - latency: 165ms
2. https://chronik-native2.fabien.cash - latency: 580ms
3. https://chronik-native3.fabien.cash - latency: 647ms
4. https://chronik.e.cash - latency: 896ms
5. https://chronik1.alitayin.com - latency: >1000ms
6. https://chronik-native1.fabien.cash - latency: >1000ms
Connecting to WebSocket...
WebSocket connected
Node 0 connected, closing immediately to test reconnection mechanism
Subscribing to block updates...
WebSocket reconnecting
WebSocket connected
Node 0 connected, closing immediately to test reconnection mechanism
WebSocket reconnecting
WebSocket connected
Node 0 connected, closing immediately to test reconnection mechanism
WebSocket reconnecting
WebSocket connected
Node 0 connected, closing immediately to test reconnection mechanism
```
This reproduces the continuous loop on node 0 before fix.
Then, if we test again with our fix, we get:
```
===== Starting WebSocket Reconnection Test =====
1. https://chronik2.alitayin.com - latency: 134ms
2. https://chronik1.alitayin.com - latency: 137ms
3. https://chronik-native2.fabien.cash - latency: 567ms
4. https://chronik-native3.fabien.cash - latency: 569ms
5. https://chronik.e.cash - latency: 869ms
6. https://chronik-native1.fabien.cash - latency: >1000ms
Connecting to WebSocket...
WebSocket connected
Node 0 connected, closing immediately to test reconnection mechanism
Subscribing to block updates...
WebSocket reconnecting
Reconnecting to node 1 with a delay of 13 ms
WebSocket connected
Node 1 connected, closing immediately to test reconnection mechanism
WebSocket reconnecting
Reconnecting to node 2 with a delay of 27 ms
WebSocket connected
Node 2 connected, closing immediately to test reconnection mechanism
WebSocket reconnecting
Reconnecting to node 3 with a delay of 41 ms
WebSocket connected
Node 3 connected, closing immediately to test reconnection mechanism
WebSocket reconnecting
Reconnecting to node 4 with a delay of 55 ms
WebSocket connected
Node 4 connected, closing immediately to test reconnection mechanism
WebSocket reconnecting
Reconnecting to node 5 with a delay of 69 ms
```
Now, node switching works smoothly. When we set the number of nodes to 1, the delay becomes 500ms.