-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_websocket.html
196 lines (177 loc) · 6.39 KB
/
test_websocket.html
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
<!DOCTYPE html>
<html>
<head>
<title>SIPREC Transcription WebSocket Client</title>
<style>
body {
font-family: sans-serif;
max-width: 1200px;
margin: 0 auto;
padding: 20px;
}
h1 {
color: #333;
}
.controls {
margin: 20px 0;
padding: 15px;
background: #f5f5f5;
border-radius: 5px;
}
#callUUID {
padding: 8px;
width: 250px;
}
button {
padding: 8px 15px;
background: #4CAF50;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
button:hover {
background: #45a049;
}
#status {
margin: 10px 0;
padding: 10px;
border-radius: 4px;
}
.connected {
background: #d4edda;
color: #155724;
}
.disconnected {
background: #f8d7da;
color: #721c24;
}
.connecting {
background: #fff3cd;
color: #856404;
}
.transcription {
margin: 10px 0;
padding: 15px;
border-radius: 4px;
border-left: 4px solid #ccc;
background: #f9f9f9;
}
.interim {
border-left-color: #ffc107;
background: #fffbf0;
}
.final {
border-left-color: #28a745;
background: #f0fff4;
}
.metadata {
font-size: 0.8em;
color: #666;
margin-top: 5px;
}
#transcriptions {
max-height: 600px;
overflow-y: auto;
border: 1px solid #ddd;
border-radius: 4px;
padding: 10px;
}
</style>
</head>
<body>
<h1>SIPREC Transcription WebSocket Client</h1>
<div class="controls">
<p>Connect to receive real-time transcriptions. Optionally specify a Call UUID to get transcriptions for a specific call only.</p>
<input type="text" id="callUUID" placeholder="Call UUID (optional)">
<button id="connect">Connect</button>
<button id="disconnect" disabled>Disconnect</button>
<div id="status" class="disconnected">Disconnected</div>
</div>
<h2>Transcriptions</h2>
<button id="clear">Clear Transcriptions</button>
<div id="transcriptions"></div>
<script>
let socket;
const statusEl = document.getElementById('status');
const transcriptionsEl = document.getElementById('transcriptions');
const connectButton = document.getElementById('connect');
const disconnectButton = document.getElementById('disconnect');
const callUUIDInput = document.getElementById('callUUID');
const clearButton = document.getElementById('clear');
function updateStatus(message, type) {
statusEl.textContent = message;
statusEl.className = type;
}
function connect() {
// Get the WebSocket URL
const callUUID = callUUIDInput.value.trim();
let wsUrl = 'ws://localhost:9090/ws/transcriptions';
if (callUUID) {
wsUrl += '?call_uuid=' + encodeURIComponent(callUUID);
}
// Create WebSocket connection
updateStatus('Connecting...', 'connecting');
socket = new WebSocket(wsUrl);
// Connection opened
socket.addEventListener('open', function(event) {
updateStatus('Connected', 'connected');
connectButton.disabled = true;
disconnectButton.disabled = false;
});
// Listen for messages
socket.addEventListener('message', function(event) {
const data = JSON.parse(event.data);
displayTranscription(data);
});
// Connection closed
socket.addEventListener('close', function(event) {
updateStatus('Disconnected', 'disconnected');
connectButton.disabled = false;
disconnectButton.disabled = true;
});
// Connection error
socket.addEventListener('error', function(event) {
console.error('WebSocket error:', event);
updateStatus('Error: Could not connect to server', 'disconnected');
connectButton.disabled = false;
disconnectButton.disabled = true;
});
}
function disconnect() {
if (socket) {
socket.close();
}
}
function displayTranscription(data) {
const transcriptionDiv = document.createElement('div');
transcriptionDiv.className = 'transcription ' + (data.is_final ? 'final' : 'interim');
const textEl = document.createElement('div');
textEl.textContent = data.transcription;
transcriptionDiv.appendChild(textEl);
const metadataEl = document.createElement('div');
metadataEl.className = 'metadata';
metadataEl.textContent = 'Call UUID: ' + data.call_uuid +
' | Type: ' + (data.is_final ? 'Final' : 'Interim') +
' | Timestamp: ' + new Date(data.timestamp).toLocaleTimeString();
if (data.metadata) {
if (data.metadata.provider) {
metadataEl.textContent += ' | Provider: ' + data.metadata.provider;
}
if (data.metadata.confidence) {
metadataEl.textContent += ' | Confidence: ' + data.metadata.confidence.toFixed(2);
}
}
transcriptionDiv.appendChild(metadataEl);
transcriptionsEl.insertBefore(transcriptionDiv, transcriptionsEl.firstChild);
}
function clearTranscriptions() {
transcriptionsEl.innerHTML = '';
}
// Attach event listeners
connectButton.addEventListener('click', connect);
disconnectButton.addEventListener('click', disconnect);
clearButton.addEventListener('click', clearTranscriptions);
</script>
</body>
</html>