How to solved the share state in WebSocket? #3043
Unanswered
callmeitachi
asked this question in
Q&A
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Summary
I have the following code:
use axum::{
Extension,
extract::{ws::{Message, WebSocket, WebSocketUpgrade}, State}, response::{IntoResponse, Response}, routing::get, Router
};
use std::sync::{Arc,Mutex};
pub struct AppState {
pub data:Arc<Mutex>,
}
pub async fn handler1(ws: WebSocketUpgrade,State(state):State) -> Response {
ws.on_upgrade(|socket| handle_socket(socket,state))
}
pub async fn handle_socket(mut socket: WebSocket,state:AppState) {

while let Some(msg) = socket.recv().await {
println!("got it");
let msg = if let Ok(msg) = msg {
msg
} else {
// client disconnected
return;
};
let mut state=state.data.lock().unwrap();
}
but it can't compile?why?
axum version
axum = {version="0.7.7",features=["multipart","ws"]}
Beta Was this translation helpful? Give feedback.
All reactions