2024-04-28 04:53:00 -04:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
<title>Cards For Humanity Test Client</title>
|
|
|
|
<style>
|
|
|
|
html, body, input, textarea, button {
|
|
|
|
background-color: #111;
|
|
|
|
color: #DDD;
|
|
|
|
}
|
2024-04-29 03:32:44 -04:00
|
|
|
div {
|
|
|
|
margin-top: 1rem;
|
|
|
|
}
|
|
|
|
|
2024-04-28 04:53:00 -04:00
|
|
|
</style>
|
|
|
|
</head>
|
|
|
|
<body>
|
|
|
|
<h1>Cards For Humanity Test Client</h1>
|
2024-04-29 02:18:20 -04:00
|
|
|
<div id="status">
|
|
|
|
<p><em>Disconnected...</em></p>
|
|
|
|
</div>
|
2024-04-29 03:32:44 -04:00
|
|
|
<div>
|
|
|
|
<form id="login" onsubmit="loginSubmit();return false">
|
|
|
|
<input id="username" style="display:block; width:100px; box-sizing: border-box" type="text" placeholder="username">
|
|
|
|
<button id="join-chat" type="submit">Join Chat</button>
|
|
|
|
</form>
|
|
|
|
</div>
|
|
|
|
<div>
|
|
|
|
<form id="chat" onsubmit="chatSubmit();return false">
|
|
|
|
<textarea id="chat-history" readonly="true" wrap="soft" style="display:block; width:30rem; height:10rem; box-sizing: border-box" cols="30" rows="10"></textarea>
|
|
|
|
<input id="chat-input" style="display:block; width:30rem; box-sizing: border-box" type="text" placeholder="chat">
|
|
|
|
</form>
|
|
|
|
</div>
|
2024-04-28 04:53:00 -04:00
|
|
|
<script type="text/javascript">
|
2024-04-29 02:18:20 -04:00
|
|
|
websocket = new WebSocket("ws://localhost:3030/websocket");
|
2024-04-28 04:53:00 -04:00
|
|
|
|
2024-04-29 02:18:20 -04:00
|
|
|
function loginSubmit() {
|
|
|
|
document.getElementById("join-chat").disabled = true;
|
|
|
|
websocket.send(username.value);
|
|
|
|
};
|
2024-04-28 04:53:00 -04:00
|
|
|
|
2024-04-29 02:18:20 -04:00
|
|
|
websocket.onopen = function() {
|
2024-04-29 03:32:44 -04:00
|
|
|
console.log("connection opened",websocket.extensions, websocket.protocol, websocket.readyState);
|
2024-04-29 02:18:20 -04:00
|
|
|
document.getElementById("status").innerHTML = '<p><em>Connected!</em></p>';
|
|
|
|
}
|
2024-04-28 04:53:00 -04:00
|
|
|
|
2024-04-29 02:18:20 -04:00
|
|
|
websocket.onclose = function() {
|
|
|
|
console.log("connection closed");
|
|
|
|
// document.getElementById("join-chat").disabled = false;
|
|
|
|
document.getElementById("status").innerHTML = '<p><em>Disconnected...</em></p>';
|
|
|
|
}
|
2024-04-28 04:53:00 -04:00
|
|
|
|
2024-04-29 02:18:20 -04:00
|
|
|
websocket.onmessage = function(e) {
|
2024-04-29 03:32:44 -04:00
|
|
|
const history = document.getElementById("chat-history")
|
2024-04-29 02:18:20 -04:00
|
|
|
console.log("received message: "+e.data);
|
2024-04-29 03:32:44 -04:00
|
|
|
history.value += e.data+"\r";
|
|
|
|
history.scrollTop = history.scrollHeight;
|
2024-04-29 02:18:20 -04:00
|
|
|
}
|
2024-04-28 04:53:00 -04:00
|
|
|
|
2024-04-29 02:18:20 -04:00
|
|
|
function chatSubmit() {
|
|
|
|
let input = document.getElementById("chat-input");
|
|
|
|
websocket.send(input.value);
|
|
|
|
input.value = "";
|
|
|
|
};
|
2024-04-29 03:32:44 -04:00
|
|
|
|
|
|
|
websocket.addEventListener("error", (event) => {
|
|
|
|
console.log("WebSocket error: ", event);
|
|
|
|
});
|
2024-04-28 04:53:00 -04:00
|
|
|
</script>
|
|
|
|
</body>
|
|
|
|
</html>
|