Add super simple client example

This commit is contained in:
Christopher Cookman 2025-04-17 23:46:05 -06:00
parent 21ddfe4160
commit 4c53a02d58
2 changed files with 37 additions and 1 deletions

36
client-example/index.js Normal file
View file

@ -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
});

View file

@ -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