Files
huly-platform/scripts/TEST.html
T
2025-08-19 13:41:22 +03:00

137 lines
4.3 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>
<button onclick="clean()">❌clear</button>
<p>
<button onclick="place()" data='{"type":"put", "key":"00000000-0000-0000-0000-000000000001/foo/bar1", "data": "hello 1", "TTL":5, "correlation": "abc123"}'>PUT 1</button>
<button onclick="place()" data='{"type":"put", "key":"00000000-0000-0000-0000-000000000001/foo/bar2", "data": "hello 2", "TTL":9, "correlation": "abc123"}'>PUT 2</button>
<button onclick="place()" data='{"type":"delete", "key":"00000000-0000-0000-0000-000000000001/foo/bar1"}'>DEL 1</button>
<button onclick="place()" data='{"type":"delete", "key":"00000000-0000-0000-0000-000000000001/foo/bar2"}'>DEL 2</button>
<button onclick="place()" data='{"type":"get", "key":"00000000-0000-0000-0000-000000000001/foo/bar1"}'>GET 1</button>
<button onclick="place()" data='{"type":"get", "key":"00000000-0000-0000-0000-000000000001/foo/bar2"}'>GET 2</button>
<button onclick="place()" data='{"type":"sub", "key":"00000000-0000-0000-0000-000000000001/foo/bar1"}'>SUB 1</button>
<button onclick="place()" data='{"type":"sub", "key":"00000000-0000-0000-0000-000000000001/foo/bar2"}'>SUB 2</button>
<button onclick="place()" data='{"type":"unsub", "key":"00000000-0000-0000-0000-000000000001/foo/bar1"}'>UNSUB 1</button>
<button onclick="place()" data='{"type":"unsub", "key":"00000000-0000-0000-0000-000000000001/foo/bar2"}'>UNSUB 2</button>
<button onclick="place()" data='{"type":"unsub", "key":"*"}'>UNSUB *</button>
<button onclick="place()" data='{"type": "sublist" }'>SUBLIST</button>
<div id="output">Waiting for server response...</div>
<script>
const output = document.getElementById("output");
const textarea = document.getElementById("jsonInput");
// const workspace="00000000-0000-0000-0000-000000000001";
let token="eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhY2NvdW50IjoiYWFhYWFhYWEtYmJiYi1jY2NjLWRkZGQtZWVlZWVlZWVlZWVlIiwiZXh0cmEiOnsic2VydmljZSI6ImFjY291bnQifSwid29ya3NwYWNlIjoiMDAwMDAwMDAtMDAwMC0wMDAwLTAwMDAtMDAwMDAwMDAwMDAxIn0.ZrwMvv_0CjuKeF2CkmHyMK2vHd9Ro4M3kHZcZBrBxZQ";
let ws = new WebSocket(`ws://localhost:8095/ws?token=${token}`);
// let ws = new WebSocket(`ws://localhost:8095/ws`);
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"); sendMessage(); }
function clean() { output.textContent = ""; }
</script>
</body>
</html>