| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import WebSocket from 'ws'; |
| import * as readline from 'readline'; |
|
|
| const SESSION_ID = 'test-session-123'; |
| const WS_URL = process.env.WS_URL || 'wss://medagen-backend.hf.space/ws/chat'; |
| const API_URL = process.env.API_URL || 'https://medagen-backend.hf.space/api/health-check'; |
| const fullWsUrl = `${WS_URL}?session=${SESSION_ID}`; |
|
|
| console.log('π§ͺ WebSocket Interactive Test Client'); |
| console.log('====================================\n'); |
| console.log(`π‘ WebSocket URL: ${fullWsUrl}`); |
| console.log(`π API URL: ${API_URL}\n`); |
| console.log(`Connecting to: ${fullWsUrl}\n`); |
|
|
| const ws = new WebSocket(fullWsUrl); |
|
|
| ws.on('open', () => { |
| console.log('β
WebSocket connected successfully!\n'); |
| console.log('Listening for messages...\n'); |
| console.log('Commands:'); |
| console.log(' q - Quit'); |
| console.log(' t - Test sending a triage request'); |
| console.log(' p - Send ping\n'); |
| }); |
|
|
| ws.on('message', (data) => { |
| try { |
| const message = JSON.parse(data.toString()); |
| console.log('\nπ© Received message:'); |
| console.log('Type:', message.type); |
| console.log('Data:', JSON.stringify(message, null, 2)); |
| console.log(''); |
| } catch (error) { |
| console.error('β Error parsing message:', error); |
| console.log('Raw data:', data.toString()); |
| } |
| }); |
|
|
| ws.on('error', (error) => { |
| console.error('β WebSocket error:', error.message); |
| }); |
|
|
| ws.on('close', (code, reason) => { |
| console.log(`\nπ WebSocket closed: code=${code}, reason=${reason || 'none'}`); |
| process.exit(0); |
| }); |
|
|
| |
| const rl = readline.createInterface({ |
| input: process.stdin, |
| output: process.stdout, |
| prompt: '> ', |
| }); |
|
|
| rl.prompt(); |
|
|
| rl.on('line', async (line) => { |
| const cmd = line.trim().toLowerCase(); |
|
|
| if (cmd === 'q' || cmd === 'quit') { |
| console.log('π Closing connection...'); |
| ws.close(); |
| rl.close(); |
| } else if (cmd === 'p' || cmd === 'ping') { |
| ws.send(JSON.stringify({ type: 'ping', timestamp: new Date().toISOString() })); |
| console.log('π Sent ping'); |
| } else if (cmd === 't' || cmd === 'test') { |
| console.log('π€ Sending test triage request...'); |
|
|
| |
| const fetch = await import('node-fetch').then(m => m.default); |
|
|
| try { |
| const response = await fetch(API_URL, { |
| method: 'POST', |
| headers: { |
| 'Content-Type': 'application/json', |
| }, |
| body: JSON.stringify({ |
| text: 'eye pain', |
| user_id: 'anonymous', |
| session_id: SESSION_ID, |
| }), |
| }); |
|
|
| const result = await response.json(); |
| console.log('β
Triage request sent successfully'); |
| console.log('Response:', JSON.stringify(result, null, 2)); |
| console.log('\nWatch for WebSocket messages above!\n'); |
| } catch (error) { |
| console.error('β Error sending triage request:', error.message); |
| } |
| } |
|
|
| rl.prompt(); |
| }); |
|
|
| |
| process.on('SIGINT', () => { |
| console.log('\n\nπ Closing connection...'); |
| ws.close(); |
| rl.close(); |
| process.exit(0); |
| }); |
|
|