From 4c53a02d586f469db66a2dbe454037dd0be875e5 Mon Sep 17 00:00:00 2001 From: Chris Chrome Date: Thu, 17 Apr 2025 23:46:05 -0600 Subject: [PATCH] Add super simple client example --- client-example/index.js | 36 ++++++++++++++++++++++++++++++++++++ index.js | 2 +- 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 client-example/index.js diff --git a/client-example/index.js b/client-example/index.js new file mode 100644 index 0000000..4543133 --- /dev/null +++ b/client-example/index.js @@ -0,0 +1,36 @@ +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 +}); \ No newline at end of file diff --git a/index.js b/index.js index 8467084..f798538 100644 --- a/index.js +++ b/index.js @@ -402,7 +402,7 @@ xmpp.on("stanza", (stanza) => { }); xmpp.on("status", (status) => { - console.log(`${colors.cyan("[INFO]")} XMPP Status`); + console.log(`${colors.cyan("[INFO]")} XMPP Status is ${status}`); // Broadcast a message to all connected WebSocket clients wsConnections.forEach((connection) => { if (connection.ws.readyState === 1) { // Ensure the socket is open