66 lines
2.0 KiB
HTML
66 lines
2.0 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="utf-8">
|
|
<title>Websockets echo test</title>
|
|
<link rel="stylesheet" href="./default.css">
|
|
</head>
|
|
<body>
|
|
<div class="content">
|
|
<h1 class="content-title">Websockets echo test</h1>
|
|
|
|
<div class="content-body">
|
|
<textarea id="wfc-output"
|
|
disabled></textarea>
|
|
<input autofocus id="wfc-input"></input>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
let ielt = document.getElementById('wfc-input');
|
|
let oelt = document.getElementById('wfc-output');
|
|
let ws = new WebSocket("/ws/echo");
|
|
|
|
// when user hits any key while typing in ielt
|
|
function on_input_key(evt) {
|
|
if (evt.key === 'Enter') {
|
|
// don't do default thing
|
|
evt.preventDefault();
|
|
// grab contents
|
|
let contents = ielt.value;
|
|
let trimmed = contents.trim();
|
|
// if contents are nonempty
|
|
let nonempty_contents = trimmed.length > 0;
|
|
if (nonempty_contents) {
|
|
console.log('message to server:', contents.trim());
|
|
// query backend for result
|
|
ws.send(contents.trim());
|
|
|
|
// clear input
|
|
ielt.value = '';
|
|
|
|
// add to output
|
|
oelt.value += '> ';
|
|
oelt.value += trimmed;
|
|
oelt.value += '\n';
|
|
}
|
|
}
|
|
}
|
|
|
|
function main() {
|
|
ielt.addEventListener('keydown', on_input_key);
|
|
ws.onmessage =
|
|
function (msg_evt) {
|
|
console.log('message from server:', msg_evt);
|
|
let msg_str = msg_evt.data;
|
|
oelt.value += '< ';
|
|
oelt.value += msg_str;
|
|
oelt.value += '\n';
|
|
};
|
|
}
|
|
|
|
main();
|
|
</script>
|
|
</body>
|
|
</html>
|