const WebSocket = require('ws'); function connectWebSocket() { console.log('Attempting to connect to WebSocket...'); const ws = new WebSocket('ws://localhost:3000/iem'); ws.on('open', () => { console.log('Connected to WebSocket'); ws.send(JSON.stringify({ type: 'subscribe', channel: 'botstalk' })); }); ws.on('message', (data) => { console.log(JSON.parse(data)); }); ws.on('close', () => { console.log('WebSocket connection closed. Reconnecting...'); setTimeout(connectWebSocket, 1000); // Retry after 1 second }); ws.on('error', (err) => { console.error('WebSocket error:', err); ws.close(); // Ensure the connection is closed before retrying }); } connectWebSocket(); process.on('uncaughtException', (err) => { console.error('Unhandled exception:', err.message); setTimeout(connectWebSocket, 1000); // Retry after 1 second }); process.on('unhandledRejection', (reason) => { console.error('Unhandled rejection:', reason); setTimeout(connectWebSocket, 1000); // Retry after 1 second });