mirror of
https://github.com/DioxusLabs/dioxus
synced 2024-12-23 19:13:08 +00:00
66 lines
1.6 KiB
Rust
66 lines
1.6 KiB
Rust
use dioxus::prelude::*;
|
|
use dioxus_liveview::LiveViewPool;
|
|
use salvo::affix;
|
|
use salvo::prelude::*;
|
|
use std::net::SocketAddr;
|
|
use std::sync::Arc;
|
|
|
|
fn app(cx: Scope) -> Element {
|
|
let mut num = use_state(cx, || 0);
|
|
|
|
cx.render(rsx! {
|
|
div {
|
|
"hello salvo! {num}"
|
|
button { onclick: move |_| num += 1, "Increment" }
|
|
}
|
|
})
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() {
|
|
pretty_env_logger::init();
|
|
|
|
let addr: SocketAddr = ([127, 0, 0, 1], 3030).into();
|
|
|
|
let view = LiveViewPool::new();
|
|
|
|
let router = Router::new()
|
|
.hoop(affix::inject(Arc::new(view)))
|
|
.get(index)
|
|
.push(Router::with_path("ws").get(connect));
|
|
|
|
println!("Listening on http://{}", addr);
|
|
|
|
Server::new(TcpListener::bind(addr)).serve(router).await;
|
|
}
|
|
|
|
#[handler]
|
|
fn index(_depot: &mut Depot, res: &mut Response) {
|
|
let addr: SocketAddr = ([127, 0, 0, 1], 3030).into();
|
|
res.render(Text::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"))
|
|
)));
|
|
}
|
|
|
|
#[handler]
|
|
async fn connect(
|
|
req: &mut Request,
|
|
depot: &mut Depot,
|
|
res: &mut Response,
|
|
) -> Result<(), StatusError> {
|
|
let view = depot.obtain::<Arc<LiveViewPool>>().unwrap().clone();
|
|
|
|
WebSocketUpgrade::new()
|
|
.upgrade(req, res, |ws| async move {
|
|
_ = view.launch(dioxus_liveview::salvo_socket(ws), app).await;
|
|
})
|
|
.await
|
|
}
|