Skip to content

Commit 32e12b2

Browse files
authored
Update websocket_handler.js
1 parent 0a38ee0 commit 32e12b2

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

main/frontend/public/js/websocket_handler.js

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,51 @@ class WebSocketHandler {
2626
this.ws.onopen = () => {
2727
document.getElementById('websocket-status').innerText = 'Connected';
2828
this.reconnectAttempts = 0;
29+
this.sendMessage({ type: 'subscribe', channel: 'kpi_updates' });
2930
};
3031

3132
this.ws.onmessage = (event) => {
32-
const data = JSON.parse(event.data);
33-
if (data.type === 'session_expired') {
34-
document.getElementById('output').innerText = 'Session expired. Please re-authenticate.';
35-
this.ws?.close();
36-
this.handleSessionExpiration();
37-
} else if (data.type === 'transaction_update') {
38-
document.getElementById('transaction-history').innerHTML += `
39-
<div class="p-2 border-b">
40-
<p><strong>Transaction ID:</strong> ${data.transaction_id}</p>
41-
<p><strong>Amount:</strong> ${data.amount} $WEBXOS</p>
42-
<p><strong>Destination:</strong> ${data.destination_address}</p>
43-
<p><strong>Timestamp:</strong> ${new Date(data.timestamp).toLocaleString()}</p>
44-
</div>
45-
`;
33+
try {
34+
const data = JSON.parse(event.data);
35+
if (data.type === 'session_expired') {
36+
document.getElementById('output').innerText = 'Session expired. Please re-authenticate.';
37+
this.ws?.close();
38+
this.handleSessionExpiration();
39+
} else if (data.type === 'transaction_update') {
40+
document.getElementById('transaction-history').innerHTML += `
41+
<div class="p-2 border-b">
42+
<p><strong>Transaction ID:</strong> ${data.transaction_id}</p>
43+
<p><strong>Amount:</strong> ${data.amount} $WEBXOS</p>
44+
<p><strong>Destination:</strong> ${data.destination_address}</p>
45+
<p><strong>Timestamp:</strong> ${new Date(data.timestamp).toLocaleString()}</p>
46+
</div>
47+
`;
48+
} else if (data.type === 'kpi_update') {
49+
document.getElementById('kpi-auth-success').innerText = data.data.auth_success_rate.toFixed(2);
50+
document.getElementById('kpi-auth-failures').innerText = data.data.auth_failure_count;
51+
document.getElementById('kpi-sessions').innerText = data.data.active_sessions;
52+
document.getElementById('kpi-anomalies').innerText = data.data.anomalies_detected;
53+
}
54+
} catch (error) {
55+
document.getElementById('output').innerText = `WebSocket message error: ${error.message}`;
4656
}
4757
};
4858

49-
this.ws.onclose = () => {
50-
document.getElementById('websocket-status').innerText = 'Disconnected';
59+
this.ws.onclose = (event) => {
60+
document.getElementById('websocket-status').innerText = `Disconnected: ${event.reason || 'Unknown reason'}`;
5161
this.handleReconnect(userId);
5262
};
5363

54-
this.ws.onerror = () => {
55-
document.getElementById('websocket-status').innerText = 'Error';
64+
this.ws.onerror = (error) => {
65+
document.getElementById('websocket-status').innerText = `WebSocket Error: ${error.type || 'Unknown error'}`;
66+
document.getElementById('output').innerText = `WebSocket connection failed. Retrying...`;
5667
this.ws?.close();
5768
};
5869
}
5970

6071
handleSessionExpiration() {
6172
localStorage.removeItem('access_token');
73+
localStorage.removeItem('refresh_token');
6274
document.cookie = 'session_id=; Max-Age=0; path=/;';
6375
document.getElementById('user-id').innerText = 'Not logged in';
6476
document.getElementById('execute-claude-btn').disabled = true;
@@ -69,6 +81,7 @@ class WebSocketHandler {
6981
document.getElementById('git-push-btn').disabled = true;
7082
document.getElementById('cash-out-btn').disabled = true;
7183
document.getElementById('logout-btn').disabled = true;
84+
document.getElementById('data-erasure-btn').disabled = true;
7285
document.getElementById('auth-btn').click();
7386
}
7487

@@ -81,13 +94,16 @@ class WebSocketHandler {
8194
}, this.reconnectInterval);
8295
} else {
8396
document.getElementById('websocket-status').innerText = 'Disconnected: Max reconnect attempts reached';
97+
document.getElementById('output').innerText = 'WebSocket connection failed after max retries. Please re-authenticate.';
8498
this.handleSessionExpiration();
8599
}
86100
}
87101

88102
sendMessage(message: any) {
89103
if (this.ws?.readyState === WebSocket.OPEN) {
90104
this.ws.send(JSON.stringify(message));
105+
} else {
106+
document.getElementById('output').innerText = 'Cannot send message: WebSocket is not connected';
91107
}
92108
}
93109
}

0 commit comments

Comments
 (0)