2022-12-19 21:11:49 +00:00
|
|
|
use dioxus::prelude::*;
|
|
|
|
use dioxus_liveview::LiveViewPool;
|
2022-12-19 23:29:20 +00:00
|
|
|
use salvo::affix;
|
2022-12-19 21:11:49 +00:00
|
|
|
use salvo::prelude::*;
|
|
|
|
use std::net::SocketAddr;
|
|
|
|
use std::sync::Arc;
|
2022-09-13 21:24:30 +00:00
|
|
|
|
2022-12-19 21:11:49 +00:00
|
|
|
fn app(cx: Scope) -> Element {
|
|
|
|
let mut num = use_state(cx, || 0);
|
2022-09-13 21:24:30 +00:00
|
|
|
|
2022-12-19 21:11:49 +00:00
|
|
|
cx.render(rsx! {
|
|
|
|
div {
|
|
|
|
"hello salvo! {num}"
|
|
|
|
button { onclick: move |_| num += 1, "Increment" }
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2022-09-13 21:24:30 +00:00
|
|
|
|
2022-12-19 21:11:49 +00:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
2022-09-13 21:24:30 +00:00
|
|
|
pretty_env_logger::init();
|
|
|
|
|
2022-12-19 21:11:49 +00:00
|
|
|
let addr: SocketAddr = ([127, 0, 0, 1], 3030).into();
|
|
|
|
|
|
|
|
let view = LiveViewPool::new();
|
2022-09-13 21:24:30 +00:00
|
|
|
|
|
|
|
let router = Router::new()
|
|
|
|
.hoop(affix::inject(Arc::new(view)))
|
|
|
|
.get(index)
|
2022-12-19 21:11:49 +00:00
|
|
|
.push(Router::with_path("ws").get(connect));
|
|
|
|
|
|
|
|
println!("Listening on http://{}", addr);
|
|
|
|
|
2022-09-13 21:24:30 +00:00
|
|
|
Server::new(TcpListener::bind(addr)).serve(router).await;
|
2022-12-19 21:11:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[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>
|
2023-02-24 21:41:16 +00:00
|
|
|
<head> <title>Dioxus LiveView with Salvo</title> </head>
|
2022-12-19 21:11:49 +00:00
|
|
|
<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();
|
|
|
|
|
2022-12-19 23:29:20 +00:00
|
|
|
WebSocketUpgrade::new()
|
|
|
|
.upgrade(req, res, |ws| async move {
|
2022-12-19 21:11:49 +00:00
|
|
|
_ = view.launch(dioxus_liveview::salvo_socket(ws), app).await;
|
2022-12-19 23:29:20 +00:00
|
|
|
})
|
|
|
|
.await
|
2022-09-13 21:24:30 +00:00
|
|
|
}
|