read loop

This commit is contained in:
2026-08-11 21:02:26 -04:00
parent 0438bade23
commit 180e7ec4ce
2 changed files with 31 additions and 4 deletions
+9 -2
View File
@@ -22,9 +22,16 @@ async fn main() {
async fn ws_handler(ws: WebSocketUpgrade) -> Response {
ws.on_upgrade(|socket: WebSocket| async {
match Client::initialize(socket).await {
Ok(client) => {}
Ok(mut client) => {
if let Err(e) = client.read_loop().await {
eprintln!("Failed to handle client: {e}");
} else {
println!("Client connection closed")
}
}
Err(e) => {
eprintln!("Failed to initialize client {e}")
eprintln!("Failed to initialize client: {e}")
}
}
})
+22 -2
View File
@@ -23,8 +23,9 @@ pub enum ClientMethod {
signature: String,
},
#[serde(rename = "error")]
Error { error: Cow<'static, str> },
Error {
error: Cow<'static, str>,
},
}
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -42,6 +43,25 @@ pub enum ServerMethod {
}
impl Client {
pub async fn read_loop(&mut self) -> anyhow::Result<()> {
while let Some(message) = self.read().await? {
match message {
ServerMethod::Initialize { .. } => {
self.send(ClientMethod::Error {
error: Cow::Borrowed("Already initialized"),
})
.await?;
}
ServerMethod::Error { error } => {
eprintln!("Client error: {error}");
}
}
}
Ok(())
}
pub async fn read_socket(socket: &mut WebSocket) -> anyhow::Result<Option<ServerMethod>> {
match socket.recv().await.transpose()? {
Some(Message::Text(text)) => {