2022-12-19 21:11:49 +00:00
|
|
|
use dioxus::prelude::*;
|
|
|
|
use dioxus_liveview::adapters::warp_adapter::warp_socket;
|
|
|
|
use dioxus_liveview::LiveViewPool;
|
|
|
|
use std::net::SocketAddr;
|
|
|
|
use warp::ws::Ws;
|
|
|
|
use warp::Filter;
|
|
|
|
|
|
|
|
fn app(cx: Scope) -> Element {
|
|
|
|
let mut num = use_state(cx, || 0);
|
|
|
|
|
|
|
|
cx.render(rsx! {
|
|
|
|
div {
|
|
|
|
"hello warp! {num}"
|
|
|
|
button {
|
|
|
|
onclick: move |_| num += 1,
|
|
|
|
"Increment"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-03-15 05:02:44 +00:00
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
2022-12-19 21:11:49 +00:00
|
|
|
pretty_env_logger::init();
|
2022-07-09 19:15:20 +00:00
|
|
|
|
2022-12-19 21:11:49 +00:00
|
|
|
let addr: SocketAddr = ([127, 0, 0, 1], 3030).into();
|
2022-07-09 19:15:20 +00:00
|
|
|
|
2022-12-19 21:11:49 +00:00
|
|
|
let index = warp::path::end().map(move || {
|
|
|
|
warp::reply::html(format!(
|
|
|
|
r#"
|
|
|
|
<!DOCTYPE html>
|
|
|
|
<html>
|
|
|
|
<head> <title>Dioxus LiveView with Warp</title> </head>
|
|
|
|
<body> <div id="main"></div> </body>
|
|
|
|
{glue}
|
|
|
|
</html>
|
|
|
|
"#,
|
|
|
|
glue = dioxus_liveview::interpreter_glue(&format!("ws://{addr}/ws/"))
|
|
|
|
))
|
|
|
|
});
|
|
|
|
|
|
|
|
let pool = LiveViewPool::new();
|
|
|
|
|
|
|
|
let ws = warp::path("ws")
|
|
|
|
.and(warp::ws())
|
|
|
|
.and(warp::any().map(move || pool.clone()))
|
|
|
|
.map(move |ws: Ws, pool: LiveViewPool| {
|
|
|
|
ws.on_upgrade(|ws| async move {
|
|
|
|
let _ = pool.launch(warp_socket(ws), app).await;
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
|
|
|
println!("Listening on http://{}", addr);
|
2022-03-15 05:02:44 +00:00
|
|
|
|
2022-12-19 21:11:49 +00:00
|
|
|
warp::serve(index.or(ws)).run(addr).await;
|
2022-03-15 05:02:44 +00:00
|
|
|
}
|