cards/test_client.html
2024-05-03 23:17:39 -04:00

121 lines
3.5 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Cards For Humanity Test Client</title>
<style>
html, body, input, textarea, button {
font-family: monospace;
background-color: #111;
color: #DDD;
margin: 0;
parring: 0;
}
div {
margin-top: 1rem;
}
span {
padding-left: 1rem;
}
p {
margin: 0;
padding: 0;
}
input {
display: block;
width: 10rem;
box-sizing: border-box;
}
</style>
</head>
<body>
<h1>Cards For Humanity Test Client</h1>
<hr />
<div id="status">
<p><em>Disconnected...</em></p>
</div>
<div id="chat">
<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" type="text" style="width: 30rem;" placeholder="talk shit" />
</form>
</div>
<hr />
<form id="new-game" onsubmit="createGame(); return false">
<p>Username:</p>
<input id="username" type="text" placeholder="username" />
<p>Game Name:</p>
<input id="gamename" type="text" placeholder="game name" />
<button id="create-game" type="submit">Create Game</button>
</form>
<hr />
<div id="socketTest">
<h3>Socket Test</h3>
<form id="socketForm" onsubmit="socketTest(); return false">
<p>int 3000-4999</p>
<input id="testCloseCode" placeholder="code" type="number" />
<p>string 123 bytes or less</p>
<input id="testCloseReason" placeholder="reason" />
<button id="close" type="submit">Close Connection</button>
</form>
</div>
<script type="text/javascript">
socket = new WebSocket("ws://localhost:3030/websocket");
function socketTest() {
let code = testCloseCode.value;
let reason = testCloseReason.value;
socket.close(code, reason)
};
function createGame() {
let CAHPlayer = {
name: username.value,
role: 'Host',
white: [],
black: [],
};
let NewGameRequest = {
name: gamename.value,
host: CAHPlayer,
packs: [0],
};
socket.send(JSON.stringify(NewGameRequest));
};
function joinGame() {
console.log('not done yet');
}
socket.onopen = function() {
console.log("connection opened",socket.extensions, socket.protocol, socket.readyState);
document.getElementById("status").innerHTML = '<p><em>Connected!</em></p>';
}
socket.onclose = function() {
console.log("connection closed");
document.getElementById("status").innerHTML = '<p><em>Disconnected...</em></p>';
document.getElementById("chat-history").value = "";
}
socket.onmessage = function(e) {
const history = document.getElementById("chat-history")
console.log("received message: "+e.data);
history.value += e.data+"\r";
history.scrollTop = history.scrollHeight;
}
function chatSubmit() {
let input = document.getElementById("chat-input");
socket.send(input.value);
input.value = "";
};
socket.addEventListener("error", (event) => {
console.log("WebSocket error: ", event);
});
</script>
</body>
</html>