message handling

This commit is contained in:
Adam 2024-05-02 00:39:34 -04:00
parent 4adec7dc2c
commit 5eeefa9d8c
3 changed files with 64 additions and 30 deletions

8
Cargo.lock generated
View file

@ -755,18 +755,18 @@ checksum = "94143f37725109f92c262ed2cf5e59bce7498c01bcc1502d7b9afe439a4e9f49"
[[package]]
name = "serde"
version = "1.0.199"
version = "1.0.200"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c9f6e76df036c77cd94996771fb40db98187f096dd0b9af39c6c6e452ba966a"
checksum = "ddc6f9cc94d67c0e21aaf7eda3a010fd3af78ebf6e096aa6e2e13c79749cce4f"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.199"
version = "1.0.200"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "11bd257a6541e141e42ca6d24ae26f7714887b47e89aa739099104c7e4d3b7fc"
checksum = "856f046b9400cee3c8c94ed572ecdb752444c24528c035cd35882aad6f492bcb"
dependencies = [
"proc-macro2",
"quote",

View file

@ -29,13 +29,11 @@ pub async fn websocket(stream: WebSocket, state: Arc<AppState>) {
Message::Text(text) => {
tracing::debug!("Text: {}", text);
// let message_str: &str = message.to_text().unwrap();
tracing::debug!(
"{:#?}",
serde_json::from_str::<NewGameRequest>(&text)
.unwrap()
.host
.name
);
if let Ok(new_game) = serde_json::from_str::<NewGameRequest>(&text) {
tracing::debug!("{:#?}", new_game);
} else {
tracing::debug!("Not JSON!");
}
}
Message::Binary(data) => {
tracing::debug!("Binary: {:?}", data)

View file

@ -5,6 +5,7 @@
<title>Cards For Humanity Test Client</title>
<style>
html, body, input, textarea, button {
font-family: monospace;
background-color: #111;
color: #DDD;
}
@ -15,40 +16,72 @@
margin: 0;
padding: 0;
}
input {
display: block;
width: 10rem;
box-sizing: border-box;
}
</style>
</head>
<body>
<h1>Cards For Humanity Test Client</h1>
<hr />
<h3>Status</h3>
<div id="status">
<p><em>Disconnected...</em></p>
</div>
<div style="display: flex;">
<span>
<p>Username</p>
<p>Game Name</p>
</span>
<span>
<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>
<hr />
<div id="newGame">
<h3>Create Game</h3>
<form id="new-game" onsubmit="createGame(); return false">
<input id="username" style="display:block; width:100px; box-sizing: border-box" type="text" placeholder="username">
<input id="gamename" style="display:block; width:100px; box-sizing: border-box" type="text" placeholder="game name">
<br />
<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>
</span>
</div>
<div>
<hr />
<div id="joinGame">
<h3>Join Game</h3>
<form id="new-game" onsubmit="joinGame(); 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>
</div>
<hr />
<div id="chat">
<h3>Chat</h3>
<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">
<textarea id="chat-history" readonly="true" wrap="soft" style="display:block; width:30rem; height:10rem; box-sizing: border-box" cols="30" rows="10">Not in game</textarea>
<input id="chat-input" type="text" style="width: 30rem;" placeholder="chat" />
</form>
</div>
<script type="text/javascript">
socket = new WebSocket("ws://localhost:3030/websocket");
socket.binaryType = "ArrayBuffer";
function createGame() {
document.getElementById("create-game").disabled = true;
function socketTest() {
let code = testCloseCode.value;
let reason = testCloseReason.value;
socket.close(code, reason)
};
function createGame() {
let CAHPlayer = {
name: username.value,
role: 'Host',
@ -65,6 +98,10 @@
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>';
@ -72,7 +109,6 @@
socket.onclose = function() {
console.log("connection closed");
// document.getElementById("join-chat").disabled = false;
document.getElementById("status").innerHTML = '<p><em>Disconnected...</em></p>';
}