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]] [[package]]
name = "serde" name = "serde"
version = "1.0.199" version = "1.0.200"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0c9f6e76df036c77cd94996771fb40db98187f096dd0b9af39c6c6e452ba966a" checksum = "ddc6f9cc94d67c0e21aaf7eda3a010fd3af78ebf6e096aa6e2e13c79749cce4f"
dependencies = [ dependencies = [
"serde_derive", "serde_derive",
] ]
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.199" version = "1.0.200"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "11bd257a6541e141e42ca6d24ae26f7714887b47e89aa739099104c7e4d3b7fc" checksum = "856f046b9400cee3c8c94ed572ecdb752444c24528c035cd35882aad6f492bcb"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",

View file

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

View file

@ -5,6 +5,7 @@
<title>Cards For Humanity Test Client</title> <title>Cards For Humanity Test Client</title>
<style> <style>
html, body, input, textarea, button { html, body, input, textarea, button {
font-family: monospace;
background-color: #111; background-color: #111;
color: #DDD; color: #DDD;
} }
@ -15,40 +16,72 @@
margin: 0; margin: 0;
padding: 0; padding: 0;
} }
input {
display: block;
width: 10rem;
box-sizing: border-box;
}
</style> </style>
</head> </head>
<body> <body>
<h1>Cards For Humanity Test Client</h1> <h1>Cards For Humanity Test Client</h1>
<hr />
<h3>Status</h3>
<div id="status"> <div id="status">
<p><em>Disconnected...</em></p> <p><em>Disconnected...</em></p>
</div> </div>
<div style="display: flex;"> <hr />
<span> <div id="socketTest">
<p>Username</p> <h3>Socket Test</h3>
<p>Game Name</p> <form id="socketForm" onsubmit="socketTest(); return false">
</span> <p>int 3000-4999</p>
<span> <input id="testCloseCode" placeholder="code" type="number" />
<form id="new-game" onsubmit="createGame();return false"> <p>string 123 bytes or less</p>
<input id="username" style="display:block; width:100px; box-sizing: border-box" type="text" placeholder="username"> <input id="testCloseReason" placeholder="reason" />
<input id="gamename" style="display:block; width:100px; box-sizing: border-box" type="text" placeholder="game name"> <button id="close" type="submit">Close Connection</button>
<br /> </form>
<button id="create-game" type="submit">Create Game</button>
</form>
</span>
</div> </div>
<div> <hr />
<div id="newGame">
<h3>Create Game</h3>
<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>
</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"> <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> <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" style="display:block; width:30rem; box-sizing: border-box" type="text" placeholder="chat"> <input id="chat-input" type="text" style="width: 30rem;" placeholder="chat" />
</form> </form>
</div> </div>
<script type="text/javascript"> <script type="text/javascript">
socket = new WebSocket("ws://localhost:3030/websocket"); socket = new WebSocket("ws://localhost:3030/websocket");
socket.binaryType = "ArrayBuffer"; socket.binaryType = "ArrayBuffer";
function createGame() { function socketTest() {
document.getElementById("create-game").disabled = true; let code = testCloseCode.value;
let reason = testCloseReason.value;
socket.close(code, reason)
};
function createGame() {
let CAHPlayer = { let CAHPlayer = {
name: username.value, name: username.value,
role: 'Host', role: 'Host',
@ -65,6 +98,10 @@
socket.send(JSON.stringify(NewGameRequest)); socket.send(JSON.stringify(NewGameRequest));
}; };
function joinGame() {
console.log('not done yet');
}
socket.onopen = function() { socket.onopen = function() {
console.log("connection opened",socket.extensions, socket.protocol, socket.readyState); console.log("connection opened",socket.extensions, socket.protocol, socket.readyState);
document.getElementById("status").innerHTML = '<p><em>Connected!</em></p>'; document.getElementById("status").innerHTML = '<p><em>Connected!</em></p>';
@ -72,7 +109,6 @@
socket.onclose = function() { socket.onclose = function() {
console.log("connection closed"); console.log("connection closed");
// document.getElementById("join-chat").disabled = false;
document.getElementById("status").innerHTML = '<p><em>Disconnected...</em></p>'; document.getElementById("status").innerHTML = '<p><em>Disconnected...</em></p>';
} }