warp.rs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. use dioxus::prelude::*;
  2. use dioxus_liveview::warp_adapter::warp_socket;
  3. use dioxus_liveview::LiveViewPool;
  4. use std::net::SocketAddr;
  5. use warp::ws::Ws;
  6. use warp::Filter;
  7. fn app() -> Element {
  8. let mut num = use_signal(|| 0);
  9. rsx! {
  10. div {
  11. "hello warp! {num}"
  12. button {
  13. onclick: move |_| num += 1,
  14. "Increment"
  15. }
  16. }
  17. }
  18. }
  19. #[tokio::main]
  20. async fn main() {
  21. pretty_env_logger::init();
  22. let addr: SocketAddr = ([127, 0, 0, 1], 3030).into();
  23. let index = warp::path::end().map(move || {
  24. warp::reply::html(format!(
  25. r#"
  26. <!DOCTYPE html>
  27. <html>
  28. <head> <title>Dioxus LiveView with Warp</title> </head>
  29. <body> <div id="main"></div> </body>
  30. {glue}
  31. </html>
  32. "#,
  33. glue = dioxus_liveview::interpreter_glue(&format!("ws://{addr}/ws/"))
  34. ))
  35. });
  36. let pool = LiveViewPool::new();
  37. let ws = warp::path("ws")
  38. .and(warp::ws())
  39. .and(warp::any().map(move || pool.clone()))
  40. .map(move |ws: Ws, pool: LiveViewPool| {
  41. ws.on_upgrade(|ws| async move {
  42. let _ = pool.launch(warp_socket(ws), app).await;
  43. })
  44. });
  45. println!("Listening on http://{}", addr);
  46. warp::serve(index.or(ws)).run(addr).await;
  47. }