44 lines
1.1 KiB
JavaScript
44 lines
1.1 KiB
JavaScript
const { SSEClient } = require('sse-client');
|
|
|
|
// Create a new SSE client with SSL certificate validation bypass
|
|
const client = new SSEClient('https://api.example.com/events/orders/123', {
|
|
headers: {
|
|
'X-Signed-Auth': 'your-auth-token'
|
|
},
|
|
skipSSLValidation: true, // Bypass SSL certificate validation
|
|
debug: true
|
|
});
|
|
|
|
// Listen for order state changes
|
|
client.on('orderStateChanged', (event) => {
|
|
try {
|
|
const data = event.parsedData;
|
|
console.log('Order state changed:', data);
|
|
|
|
// If order is ended, close the connection
|
|
if (data.currentState && data.currentState.orderStatus === 1111) {
|
|
console.log(`Order ended, closing SSE connection`);
|
|
client.close();
|
|
}
|
|
} catch (err) {
|
|
console.error('Error parsing order state data:', err);
|
|
}
|
|
});
|
|
|
|
// Listen for connection errors
|
|
client.on('error', (event) => {
|
|
console.error('SSE connection error:', event.detail);
|
|
});
|
|
|
|
// Connect to the SSE endpoint
|
|
client.connect();
|
|
|
|
// Handle process termination
|
|
process.on('SIGINT', () => {
|
|
console.log('Closing SSE connection');
|
|
client.close();
|
|
process.exit(0);
|
|
});
|
|
|
|
console.log('Listening for order events. Press Ctrl+C to exit.');
|