68 lines
1.8 KiB
HTML
68 lines
1.8 KiB
HTML
|
|
<!DOCTYPE html>
|
||
|
|
<html>
|
||
|
|
<head>
|
||
|
|
<title>WebSocket Test</title>
|
||
|
|
<meta charset="utf-8">
|
||
|
|
</head>
|
||
|
|
<body>
|
||
|
|
<h1>DRS9 Dashboard WebSocket Test</h1>
|
||
|
|
<div>
|
||
|
|
<input type="text" id="deviceId" placeholder="Device ID (optional)" value="1">
|
||
|
|
<button onclick="connect()">Connect</button>
|
||
|
|
<button onclick="disconnect()">Disconnect</button>
|
||
|
|
</div>
|
||
|
|
<div>
|
||
|
|
<input type="text" id="message" placeholder="Message (e.g., {\"type\":\"ping\"})" value='{"type":"ping"}'>
|
||
|
|
<button onclick="send()">Send</button>
|
||
|
|
</div>
|
||
|
|
<pre id="log"></pre>
|
||
|
|
|
||
|
|
<script>
|
||
|
|
let ws = null;
|
||
|
|
|
||
|
|
function log(msg) {
|
||
|
|
const el = document.getElementById('log');
|
||
|
|
el.textContent += new Date().toLocaleTimeString() + ': ' + msg + '\n';
|
||
|
|
}
|
||
|
|
|
||
|
|
function connect() {
|
||
|
|
const deviceId = document.getElementById('deviceId').value;
|
||
|
|
const url = 'ws://localhost:5264/ws' + (deviceId ? '?deviceId=' + deviceId : '');
|
||
|
|
log('Connecting to ' + url);
|
||
|
|
|
||
|
|
ws = new WebSocket(url);
|
||
|
|
|
||
|
|
ws.onopen = () => {
|
||
|
|
log('Connected!');
|
||
|
|
};
|
||
|
|
|
||
|
|
ws.onmessage = (e) => {
|
||
|
|
log('Received: ' + e.data);
|
||
|
|
};
|
||
|
|
|
||
|
|
ws.onerror = (e) => {
|
||
|
|
log('Error: ' + e);
|
||
|
|
};
|
||
|
|
|
||
|
|
ws.onclose = () => {
|
||
|
|
log('Disconnected');
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
function disconnect() {
|
||
|
|
if (ws) ws.close();
|
||
|
|
}
|
||
|
|
|
||
|
|
function send() {
|
||
|
|
if (!ws || ws.readyState !== WebSocket.OPEN) {
|
||
|
|
log('Not connected');
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
const msg = document.getElementById('message').value;
|
||
|
|
log('Sending: ' + msg);
|
||
|
|
ws.send(msg);
|
||
|
|
}
|
||
|
|
</script>
|
||
|
|
</body>
|
||
|
|
</html>
|