Add super simple client example
This commit is contained in:
parent
21ddfe4160
commit
4c53a02d58
36
client-example/index.js
Normal file
36
client-example/index.js
Normal 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
|
||||||
|
});
|
2
index.js
2
index.js
|
@ -402,7 +402,7 @@ xmpp.on("stanza", (stanza) => {
|
||||||
});
|
});
|
||||||
|
|
||||||
xmpp.on("status", (status) => {
|
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
|
// Broadcast a message to all connected WebSocket clients
|
||||||
wsConnections.forEach((connection) => {
|
wsConnections.forEach((connection) => {
|
||||||
if (connection.ws.readyState === 1) { // Ensure the socket is open
|
if (connection.ws.readyState === 1) { // Ensure the socket is open
|
||||||
|
|
Loading…
Reference in a new issue