74 lines
2.1 KiB
TypeScript
74 lines
2.1 KiB
TypeScript
import { SDKSignerClient, MessageType } from '../src/index';
|
|
|
|
async function basicExample() {
|
|
// Use environment variables for configuration, with fallbacks
|
|
const serverUrl = process.env.SERVER_URL || 'ws://localhost:9090';
|
|
const apiKey = process.env.API_KEY || 'your-api-key-change-this';
|
|
|
|
console.log(`🔧 Testing against server: ${serverUrl}`);
|
|
console.log(`🔑 Using API key: ${apiKey.substring(0, 10)}...`);
|
|
console.log('');
|
|
|
|
// Create client instance
|
|
const client = new SDKSignerClient({
|
|
url: serverUrl,
|
|
apiKey: apiKey
|
|
});
|
|
|
|
// Set up event handlers
|
|
client.on('open', () => {
|
|
console.log('✅ Connected to SDK Signer server');
|
|
});
|
|
|
|
client.on('close', () => {
|
|
console.log('🔌 Disconnected from server');
|
|
});
|
|
|
|
client.on('error', (error: Error) => {
|
|
console.error('❌ Error:', error.message);
|
|
});
|
|
|
|
client.on('message', (response: any) => {
|
|
console.log('📨 Received:', response);
|
|
});
|
|
|
|
try {
|
|
// Connect to server
|
|
console.log('🔗 Connecting to server...');
|
|
await client.connect();
|
|
|
|
// Wait for server to send LISTENING message
|
|
console.log('👂 Waiting for server LISTENING message...');
|
|
const listeningResponse = await client.waitForListening();
|
|
console.log('✅ Server listening:', listeningResponse);
|
|
|
|
// Example: Notify an update
|
|
console.log('📢 Notifying update...');
|
|
const updateResponse = await client.notifyUpdate('process-123', 'state-456');
|
|
console.log('✅ Update response:', updateResponse);
|
|
|
|
// Example: Validate a state
|
|
console.log('✅ Validating state...');
|
|
const validateResponse = await client.validateState('process-123', 'state-456');
|
|
console.log('✅ Validation response:', validateResponse);
|
|
|
|
// Wait a bit before disconnecting
|
|
await new Promise(resolve => setTimeout(resolve, 2000));
|
|
|
|
} catch (error) {
|
|
console.error('❌ Error in example:', error);
|
|
process.exit(1); // Exit with error code for CI/CD
|
|
} finally {
|
|
// Disconnect
|
|
console.log('🔌 Disconnecting...');
|
|
client.disconnect();
|
|
}
|
|
}
|
|
|
|
// Run the example
|
|
if (require.main === module) {
|
|
basicExample().catch(console.error);
|
|
}
|
|
|
|
export { basicExample };
|