Repository navigation
Expand file tree
/
Copy pathclient_test.html
More file actions
251 lines (227 loc) · 8.96 KB
/
Copy pathclient_test.html
File metadata and controls
251 lines (227 loc) · 8.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>ASR WebSocket Client Test</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
.container {
border: 1px solid #ddd;
border-radius: 8px;
padding: 20px;
margin: 20px 0;
}
button {
background-color: #007bff;
color: white;
border: none;
padding: 10px 20px;
border-radius: 4px;
cursor: pointer;
margin: 5px;
}
button:hover {
background-color: #0056b3;
}
button:disabled {
background-color: #6c757d;
cursor: not-allowed;
}
#output {
background-color: #f8f9fa;
border: 1px solid #e9ecef;
border-radius: 4px;
padding: 10px;
height: 200px;
overflow-y: auto;
font-family: monospace;
white-space: pre-wrap;
}
.status {
padding: 5px 10px;
border-radius: 4px;
margin: 5px 0;
}
.connected { background-color: #d4edda; color: #155724; }
.disconnected { background-color: #f8d7da; color: #721c24; }
.transcript { background-color: #d1ecf1; color: #0c5460; }
.error { background-color: #f8d7da; color: #721c24; }
</style>
</head>
<body>
<h1>ASR WebSocket Client Test</h1>
<div class="container">
<h3>Connection</h3>
<div id="status" class="status disconnected">Disconnected</div>
<button id="connectBtn" onclick="connect()">Connect</button>
<button id="disconnectBtn" onclick="disconnect()" disabled>Disconnect</button>
</div>
<div class="container">
<h3>ASR Session</h3>
<button id="startBtn" onclick="startSession()" disabled>Start Session</button>
<button id="stopBtn" onclick="stopSession()" disabled>Stop Session</button>
<button onclick="sendPing()" disabled id="pingBtn">Send Ping</button>
</div>
<div class="container">
<h3>Test Audio</h3>
<button onclick="sendTestAudio()" disabled id="audioBtn">Send Test Audio Data</button>
<p><small>Sends mock binary audio data to test the pipeline</small></p>
</div>
<div class="container">
<h3>Output</h3>
<div id="output"></div>
<button onclick="clearOutput()">Clear Output</button>
</div>
<script>
let ws = null;
let sessionId = null;
function log(message, type = 'info') {
const output = document.getElementById('output');
const timestamp = new Date().toLocaleTimeString();
const className = type === 'error' ? 'error' : type === 'transcript' ? 'transcript' : '';
output.innerHTML += `<div class="${className}">[${timestamp}] ${message}</div>`;
output.scrollTop = output.scrollHeight;
}
function updateStatus(connected) {
const status = document.getElementById('status');
const connectBtn = document.getElementById('connectBtn');
const disconnectBtn = document.getElementById('disconnectBtn');
const startBtn = document.getElementById('startBtn');
const stopBtn = document.getElementById('stopBtn');
const pingBtn = document.getElementById('pingBtn');
const audioBtn = document.getElementById('audioBtn');
if (connected) {
status.textContent = 'Connected';
status.className = 'status connected';
connectBtn.disabled = true;
disconnectBtn.disabled = false;
startBtn.disabled = false;
pingBtn.disabled = false;
} else {
status.textContent = 'Disconnected';
status.className = 'status disconnected';
connectBtn.disabled = false;
disconnectBtn.disabled = true;
startBtn.disabled = true;
stopBtn.disabled = true;
pingBtn.disabled = true;
audioBtn.disabled = true;
sessionId = null;
}
}
function connect() {
try {
ws = new WebSocket('ws://127.0.0.1:8081/ws');
ws.onopen = function(event) {
log('Connected to ASR server');
updateStatus(true);
};
ws.onmessage = function(event) {
try {
const message = JSON.parse(event.data);
log(`Received: ${JSON.stringify(message, null, 2)}`);
switch (message.type) {
case 'ready':
sessionId = message.session_id;
document.getElementById('stopBtn').disabled = false;
document.getElementById('audioBtn').disabled = false;
log(`Session ready: ${sessionId}`, 'transcript');
break;
case 'transcript':
log(`Transcript: "${message.text}" (final: ${message.is_final})`, 'transcript');
break;
case 'error':
log(`Error: ${message.message}`, 'error');
break;
case 'pong':
log('Received pong');
break;
case 'session_ended':
log(`Session ended: ${message.session_id}`);
document.getElementById('stopBtn').disabled = true;
document.getElementById('audioBtn').disabled = true;
sessionId = null;
break;
}
} catch (e) {
log(`Failed to parse message: ${event.data}`, 'error');
}
};
ws.onclose = function(event) {
log('Connection closed');
updateStatus(false);
};
ws.onerror = function(error) {
log(`WebSocket error: ${error}`, 'error');
};
} catch (error) {
log(`Connection failed: ${error}`, 'error');
}
}
function disconnect() {
if (ws) {
ws.close();
ws = null;
}
}
function startSession() {
if (ws && ws.readyState === WebSocket.OPEN) {
const message = {
type: 'start',
sample_rate: 16000,
format: 'pcm'
};
ws.send(JSON.stringify(message));
log(`Sent: ${JSON.stringify(message)}`);
document.getElementById('startBtn').disabled = true;
}
}
function stopSession() {
if (ws && ws.readyState === WebSocket.OPEN) {
const message = { type: 'stop' };
ws.send(JSON.stringify(message));
log(`Sent: ${JSON.stringify(message)}`);
document.getElementById('startBtn').disabled = false;
}
}
function sendPing() {
if (ws && ws.readyState === WebSocket.OPEN) {
const message = { type: 'ping' };
ws.send(JSON.stringify(message));
log(`Sent: ${JSON.stringify(message)}`);
}
}
function sendTestAudio() {
if (ws && ws.readyState === WebSocket.OPEN && sessionId) {
// Generate mock audio data (16-bit PCM, 1000 samples)
const samples = 1000;
const audioData = new Int16Array(samples);
for (let i = 0; i < samples; i++) {
// Generate a simple sine wave
audioData[i] = Math.sin(2 * Math.PI * 440 * i / 16000) * 16384;
}
// Convert to base64
const bytes = new Uint8Array(audioData.buffer);
const base64 = btoa(String.fromCharCode.apply(null, bytes));
const message = {
type: 'audio',
data: base64
};
ws.send(JSON.stringify(message));
log(`Sent audio data: ${samples} samples (${bytes.length} bytes)`);
}
}
function clearOutput() {
document.getElementById('output').innerHTML = '';
}
// Update initial status
updateStatus(false);
</script>
</body>
</html>