warp.rs 930 B

12345678910111213141516171819202122232425262728293031323334
  1. #![cfg(feature = "warp")]
  2. use dioxus_core::{Element, LazyNodes, Scope};
  3. use dioxus_liveview as liveview;
  4. use warp::ws::Ws;
  5. use warp::Filter;
  6. #[tokio::main]
  7. async fn main() {
  8. pretty_env_logger::init();
  9. let addr = ([127, 0, 0, 1], 3030);
  10. // todo: compactify this routing under one liveview::app method
  11. let view = liveview::new(addr);
  12. let body = view.body();
  13. let routes = warp::path::end()
  14. .map(move || warp::reply::html(body.clone()))
  15. .or(warp::path("app")
  16. .and(warp::ws())
  17. .and(warp::any().map(move || view.clone()))
  18. .map(|ws: Ws, view: liveview::Liveview| {
  19. ws.on_upgrade(|socket| async move {
  20. view.upgrade(socket, app).await;
  21. })
  22. }));
  23. warp::serve(routes).run(addr).await;
  24. }
  25. fn app(cx: Scope) -> Element {
  26. cx.render(LazyNodes::new(|f| f.text(format_args!("hello world!"))))
  27. }