Files
attune/scripts/test-websocket.html
2026-02-04 17:46:30 -06:00

332 lines
11 KiB
HTML

<!DOCTYPE html>
<!--
Attune WebSocket Test Page
This tool tests WebSocket connectivity to the Attune notifier service.
Usage:
1. Ensure notifier service is running: `make run-notifier`
2. Open this file in a web browser: `open scripts/test-websocket.html`
3. Click "Connect" to establish WebSocket connection
4. Monitor real-time event notifications in the log
Features:
- Live connection status indicator
- Real-time message statistics (total messages, event count, connection time)
- Color-coded message log (info, events, errors)
- Manual connect/disconnect controls
- Subscription to event notifications
Default URL: ws://localhost:8081/ws
Change the URL in the input field before connecting if needed.
-->
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Attune WebSocket Test</title>
<style>
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
max-width: 900px;
margin: 40px auto;
padding: 20px;
background: #f5f5f5;
}
.container {
background: white;
border-radius: 8px;
padding: 30px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
h1 {
color: #333;
margin-top: 0;
}
.status {
padding: 10px 15px;
border-radius: 5px;
margin: 15px 0;
font-weight: bold;
}
.status.disconnected {
background: #fee;
color: #c33;
border-left: 4px solid #c33;
}
.status.connected {
background: #efe;
color: #3a3;
border-left: 4px solid #3a3;
}
.controls {
display: flex;
gap: 10px;
margin: 20px 0;
}
button {
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 14px;
transition: background 0.2s;
}
button.primary {
background: #4CAF50;
color: white;
}
button.primary:hover {
background: #45a049;
}
button.danger {
background: #f44336;
color: white;
}
button.danger:hover {
background: #da190b;
}
button:disabled {
background: #ccc;
cursor: not-allowed;
}
.stats {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 15px;
margin: 20px 0;
}
.stat {
background: #f9f9f9;
padding: 15px;
border-radius: 5px;
border-left: 3px solid #4CAF50;
}
.stat-label {
color: #666;
font-size: 12px;
text-transform: uppercase;
}
.stat-value {
font-size: 24px;
font-weight: bold;
color: #333;
margin-top: 5px;
}
.log {
background: #1e1e1e;
color: #d4d4d4;
padding: 15px;
border-radius: 5px;
max-height: 400px;
overflow-y: auto;
font-family: 'Courier New', monospace;
font-size: 13px;
margin-top: 20px;
}
.log-entry {
margin: 5px 0;
padding: 5px;
border-left: 3px solid transparent;
}
.log-entry.info {
border-left-color: #4CAF50;
}
.log-entry.event {
border-left-color: #2196F3;
background: rgba(33, 150, 243, 0.1);
}
.log-entry.error {
border-left-color: #f44336;
background: rgba(244, 67, 54, 0.1);
}
.timestamp {
color: #888;
margin-right: 10px;
}
input {
padding: 8px 12px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 14px;
flex: 1;
}
</style>
</head>
<body>
<div class="container">
<h1>🔌 Attune WebSocket Tester</h1></h1>
<div id="status" class="status disconnected">
⚫ Disconnected
</div>
<div class="controls">
<input type="text" id="wsUrl" value="ws://localhost:8081/ws" placeholder="WebSocket URL">
<button id="connectBtn" class="primary" onclick="connect()">Connect</button>
<button id="disconnectBtn" class="danger" onclick="disconnect()" disabled>Disconnect</button>
<button onclick="clearLog()">Clear Log</button>
</div>
<div class="stats">
<div class="stat">
<div class="stat-label">Messages Received</div>
<div class="stat-value" id="messageCount">0</div>
</div>
<div class="stat">
<div class="stat-label">Event Notifications</div>
<div class="stat-value" id="eventCount">0</div>
</div>
<div class="stat">
<div class="stat-label">Connection Time</div>
<div class="stat-value" id="connectionTime">--</div>
</div>
</div>
<h3>Message Log</h3>
<div id="log" class="log"></div>
</div>
<script>
let ws = null;
let messageCount = 0;
let eventCount = 0;
let connectionStart = null;
let connectionTimeInterval = null;
function log(message, type = 'info') {
const timestamp = new Date().toLocaleTimeString();
const logEntry = document.createElement('div');
logEntry.className = `log-entry ${type}`;
logEntry.innerHTML = `<span class="timestamp">[${timestamp}]</span>${message}`;
const logContainer = document.getElementById('log');
logContainer.appendChild(logEntry);
logContainer.scrollTop = logContainer.scrollHeight;
}
function updateStats() {
document.getElementById('messageCount').textContent = messageCount;
document.getElementById('eventCount').textContent = eventCount;
}
function updateConnectionTime() {
if (connectionStart) {
const elapsed = Math.floor((Date.now() - connectionStart) / 1000);
const minutes = Math.floor(elapsed / 60);
const seconds = elapsed % 60;
document.getElementById('connectionTime').textContent =
`${minutes}:${seconds.toString().padStart(2, '0')}`;
}
}
function setStatus(connected) {
const statusEl = document.getElementById('status');
const connectBtn = document.getElementById('connectBtn');
const disconnectBtn = document.getElementById('disconnectBtn');
if (connected) {
statusEl.className = 'status connected';
statusEl.textContent = '🟢 Connected';
connectBtn.disabled = true;
disconnectBtn.disabled = false;
connectionStart = Date.now();
connectionTimeInterval = setInterval(updateConnectionTime, 1000);
} else {
statusEl.className = 'status disconnected';
statusEl.textContent = '⚫ Disconnected';
connectBtn.disabled = false;
disconnectBtn.disabled = true;
document.getElementById('connectionTime').textContent = '--';
if (connectionTimeInterval) {
clearInterval(connectionTimeInterval);
connectionTimeInterval = null;
}
}
}
function connect() {
const url = document.getElementById('wsUrl').value;
log(`Connecting to ${url}...`, 'info');
try {
ws = new WebSocket(url);
ws.onopen = () => {
log('✅ Connected to notifier service', 'info');
setStatus(true);
// Subscribe to event notifications
const subscribeMsg = JSON.stringify({
type: 'subscribe',
filter: 'entity_type:event'
});
ws.send(subscribeMsg);
log('📡 Subscribed to entity_type:event', 'info');
};
ws.onmessage = (event) => {
messageCount++;
updateStats();
try {
const message = JSON.parse(event.data);
if (message.type === 'welcome') {
log(`👋 Welcome: ${message.message} (Client ID: ${message.client_id})`, 'info');
} else if (message.notification_type) {
// This is a notification
eventCount++;
updateStats();
const data = message.payload?.data || {};
log(
`🔔 Event #${message.entity_id}: ${message.notification_type} | ` +
`Trigger: ${data.trigger_ref || 'N/A'} | ` +
`Source: ${data.source_ref || 'N/A'}`,
'event'
);
} else {
log(`📨 Message: ${JSON.stringify(message)}`, 'info');
}
} catch (error) {
log(`❌ Failed to parse message: ${error.message}`, 'error');
}
};
ws.onerror = (error) => {
log(`❌ WebSocket error: ${error.message || 'Unknown error'}`, 'error');
};
ws.onclose = () => {
log('🔌 Connection closed', 'info');
setStatus(false);
};
} catch (error) {
log(`❌ Failed to connect: ${error.message}`, 'error');
setStatus(false);
}
}
function disconnect() {
if (ws) {
log('Disconnecting...', 'info');
ws.close();
ws = null;
}
}
function clearLog() {
document.getElementById('log').innerHTML = '';
messageCount = 0;
eventCount = 0;
updateStats();
log('Log cleared', 'info');
}
// Initialize
log('WebSocket tester ready. Click Connect to start.', 'info');
</script>
</body>
</html>