mirror of
https://github.com/DioxusLabs/dioxus
synced 2025-02-18 14:48:26 +00:00
Fixed example squiglies.
This commit is contained in:
parent
cc379920fc
commit
dfaa6c186b
5 changed files with 70 additions and 20 deletions
|
@ -35,7 +35,8 @@ axum = { version = "0.5.1", optional = true, features = ["ws"] }
|
||||||
tower = { version = "0.4.12", optional = true }
|
tower = { version = "0.4.12", optional = true }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
dioxus-liveview = { path = "./", features = ["warp"] }
|
tokio = { version = "1", features = ["full"] }
|
||||||
|
dioxus = { path = "../../" }
|
||||||
warp = "0.3"
|
warp = "0.3"
|
||||||
axum = { version = "0.5.1", features = ["ws"] }
|
axum = { version = "0.5.1", features = ["ws"] }
|
||||||
tower = "0.4.12"
|
tower = "0.4.12"
|
||||||
|
|
38
packages/liveview/examples/axum.rs
Normal file
38
packages/liveview/examples/axum.rs
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
use axum::{
|
||||||
|
extract::ws::WebSocketUpgrade, response::Html, response::IntoResponse, routing::get, Extension,
|
||||||
|
Router,
|
||||||
|
};
|
||||||
|
use dioxus_core::{Element, LazyNodes, Scope};
|
||||||
|
use dioxus_liveview::Liveview;
|
||||||
|
|
||||||
|
#[tokio::main]
|
||||||
|
async fn main() {
|
||||||
|
#[cfg(feature = "axum")]
|
||||||
|
{
|
||||||
|
pretty_env_logger::init();
|
||||||
|
|
||||||
|
let addr: std::net::SocketAddr = ([127, 0, 0, 1], 3030).into();
|
||||||
|
|
||||||
|
let view = dioxus_liveview::new(addr);
|
||||||
|
let body = view.body("<title>Dioxus Liveview</title>");
|
||||||
|
|
||||||
|
let app = Router::new()
|
||||||
|
.route("/", get(move || async { Html(body) }))
|
||||||
|
.route(
|
||||||
|
"/app",
|
||||||
|
get(move |ws: WebSocketUpgrade| async move {
|
||||||
|
ws.on_upgrade(move |socket| async move {
|
||||||
|
view.upgrade(socket, app).await;
|
||||||
|
})
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
axum::Server::bind(&addr.to_string().parse().unwrap())
|
||||||
|
.serve(app.into_make_service())
|
||||||
|
.await
|
||||||
|
.unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn app(cx: Scope) -> Element {
|
||||||
|
cx.render(LazyNodes::new(|f| f.text(format_args!("hello world!"))))
|
||||||
|
}
|
|
@ -5,6 +5,8 @@ use warp::Filter;
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
|
#[cfg(feature = "warp")]
|
||||||
|
{
|
||||||
pretty_env_logger::init();
|
pretty_env_logger::init();
|
||||||
|
|
||||||
let addr = ([127, 0, 0, 1], 3030);
|
let addr = ([127, 0, 0, 1], 3030);
|
||||||
|
@ -23,8 +25,8 @@ async fn main() {
|
||||||
view.upgrade(socket, app).await;
|
view.upgrade(socket, app).await;
|
||||||
})
|
})
|
||||||
}));
|
}));
|
||||||
|
|
||||||
warp::serve(routes).run(addr).await;
|
warp::serve(routes).run(addr).await;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn app(cx: Scope) -> Element {
|
fn app(cx: Scope) -> Element {
|
||||||
|
|
|
@ -7,14 +7,22 @@ use futures_util::{
|
||||||
};
|
};
|
||||||
use tokio::sync::mpsc;
|
use tokio::sync::mpsc;
|
||||||
use tokio_stream::wrappers::UnboundedReceiverStream;
|
use tokio_stream::wrappers::UnboundedReceiverStream;
|
||||||
|
use tokio_util::task::LocalPoolHandle;
|
||||||
|
|
||||||
pub async fn connect(socket: WebSocket, liveview: Liveview, app: fn(Scope) -> Element) {
|
#[cfg(feature = "axum")]
|
||||||
|
impl crate::Liveview {
|
||||||
|
pub async fn upgrade(&self, ws: WebSocket, app: fn(Scope) -> Element) {
|
||||||
|
connect(ws, self.pool.clone(), app).await;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn connect(socket: WebSocket, pool: LocalPoolHandle, app: fn(Scope) -> Element) {
|
||||||
let (mut user_ws_tx, mut user_ws_rx) = socket.split();
|
let (mut user_ws_tx, mut user_ws_rx) = socket.split();
|
||||||
let (event_tx, event_rx) = mpsc::unbounded_channel();
|
let (event_tx, event_rx) = mpsc::unbounded_channel();
|
||||||
let (edits_tx, edits_rx) = mpsc::unbounded_channel();
|
let (edits_tx, edits_rx) = mpsc::unbounded_channel();
|
||||||
let mut edits_rx = UnboundedReceiverStream::new(edits_rx);
|
let mut edits_rx = UnboundedReceiverStream::new(edits_rx);
|
||||||
let mut event_rx = UnboundedReceiverStream::new(event_rx);
|
let mut event_rx = UnboundedReceiverStream::new(event_rx);
|
||||||
let vdom_fut = liveview.pool.clone().spawn_pinned(move || async move {
|
let vdom_fut = pool.clone().spawn_pinned(move || async move {
|
||||||
let mut vdom = VirtualDom::new(app);
|
let mut vdom = VirtualDom::new(app);
|
||||||
let edits = vdom.rebuild();
|
let edits = vdom.rebuild();
|
||||||
let serialized = serde_json::to_string(&edits.edits).unwrap();
|
let serialized = serde_json::to_string(&edits.edits).unwrap();
|
||||||
|
|
|
@ -6,6 +6,7 @@ use tokio_stream::wrappers::UnboundedReceiverStream;
|
||||||
use tokio_util::task::LocalPoolHandle;
|
use tokio_util::task::LocalPoolHandle;
|
||||||
use warp::ws::{Message, WebSocket};
|
use warp::ws::{Message, WebSocket};
|
||||||
|
|
||||||
|
#[cfg(feature = "warp")]
|
||||||
impl crate::Liveview {
|
impl crate::Liveview {
|
||||||
pub async fn upgrade(&self, ws: warp::ws::WebSocket, app: fn(Scope) -> Element) {
|
pub async fn upgrade(&self, ws: warp::ws::WebSocket, app: fn(Scope) -> Element) {
|
||||||
connect(ws, self.pool.clone(), app).await;
|
connect(ws, self.pool.clone(), app).await;
|
||||||
|
|
Loading…
Add table
Reference in a new issue