Files
huly-platform/scripts/TEST.html
T

127 lines
2.7 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>WebSocket JSON Tester</title>
<style>
body {
font-family: sans-serif;
background: #f5f5f5;
margin: 2em;
}
h1 {
margin-bottom: 0.5em;
}
textarea {
width: 100%;
height: 200px;
font-family: monospace;
font-size: 14px;
padding: 1em;
border: 1px solid #ccc;
border-radius: 6px;
background: #fff;
resize: vertical;
}
button {
margin-top: 1em;
padding: 0.5em 1.5em;
font-size: 16px;
border: none;
border-radius: 4px;
background: #007bff;
color: white;
cursor: pointer;
}
button:hover {
background: #0056b3;
}
#output {
margin-top: 1.5em;
padding: 1em;
background: #222;
color: #0f0;
font-family: monospace;
font-size: 14px;
border-radius: 6px;
min-height: 100px;
white-space: pre-wrap;
overflow-wrap: anywhere;
}
</style>
</head>
<body>
<h1>WebSocket JSON Tester</h1>
<textarea id="jsonInput" placeholder='{
"type": "put",
"key": "foo/bar",
"data": "hello from UI",
"correlation": "abc123"
}'></textarea>
<br />
<button onclick="sendMessage()">Send JSON</button>
<p>
<button onclick="place()" data='{
"type": "put",
"key": "foo/bar1",
"data": "hello 1",
"correlation": "abc123"
}'>PUT 1</button>
<button onclick="place()" data='{
"type": "put",
"key": "foo/bar2",
"data": "hello 2",
"correlation": "abc123"
}'>PUT 2</button>
<button onclick="place()" data='{
"type": "get",
"key": "foo/bar1"
}'>GET 2</button>
<div id="output">Waiting for server response...</div>
<script>
const output = document.getElementById("output");
const textarea = document.getElementById("jsonInput");
let ws = new WebSocket("ws://localhost:8095/ws/testworkspace");
ws.onopen = () => {
output.textContent = "✅ WebSocket connected.";
};
ws.onmessage = (e) => {
output.textContent += "\n\n📥 From server:\n" + e.data;
};
ws.onerror = (e) => {
output.textContent += "\n\n❌ WebSocket error!";
console.error(e);
};
function sendMessage() {
const json = ( textarea.value || textarea.getAttribute('placeholder') ).trim();
try {
const parsed = JSON.parse(json); // validate JSON
ws.send(JSON.stringify(parsed));
output.textContent += "\n\n📤 Sent:\n" + JSON.stringify(parsed, null, 2);
} catch (e) {
output.textContent += "\n\n⚠️ Invalid JSON:\n" + e.message;
}
}
function place(event) { textarea.value = (event || window.event).target.getAttribute("data"); }
</script>
</body>
</html>