dioxus/packages/liveview/examples/axum_stress.rs

66 lines
1.6 KiB
Rust
Raw Normal View History

2023-10-09 19:28:12 +00:00
use axum::{extract::ws::WebSocketUpgrade, response::Html, routing::get, Router};
use dioxus::prelude::*;
fn app() -> Element {
2024-01-14 05:12:21 +00:00
let state = use_signal(|| 0);
use_future(|_| {
2023-10-09 19:28:12 +00:00
to_owned![state];
async move {
loop {
state += 1;
tokio::time::sleep(std::time::Duration::from_millis(1)).await;
}
}
});
2024-01-14 05:12:21 +00:00
rsx! {
2023-10-09 19:28:12 +00:00
for _ in 0..10000 {
div {
"hello axum! {state}"
}
}
2024-01-14 05:12:21 +00:00
}
2023-10-09 19:28:12 +00:00
}
#[tokio::main]
async fn main() {
pretty_env_logger::init();
let addr: std::net::SocketAddr = ([127, 0, 0, 1], 3030).into();
let view = dioxus_liveview::LiveViewPool::new();
let app = Router::new()
.route(
"/",
get(move || async move {
Html(format!(
r#"
<!DOCTYPE html>
<html>
<head> <title>Dioxus LiveView with axum</title> </head>
<body> <div id="main"></div> </body>
{glue}
</html>
"#,
glue = dioxus_liveview::interpreter_glue(&format!("ws://{addr}/ws"))
))
}),
)
.route(
"/ws",
get(move |ws: WebSocketUpgrade| async move {
ws.on_upgrade(move |socket| async move {
_ = view.launch(dioxus_liveview::axum_socket(socket), app).await;
})
}),
);
println!("Listening on http://{addr}");
axum::Server::bind(&addr.to_string().parse().unwrap())
.serve(app.into_make_service())
.await
.unwrap();
}