Replies: 2 comments 1 reply
-
Beta Was this translation helpful? Give feedback.
0 replies
-
Spawn runs on the same thread as the UI, so if it blocks the thread, the UI will also freeze. There is more documentation about async vs blocking tasks here The tokio console is often helpful for debugging this kind of issue: https://github.com/tokio-rs/console
That code will run every time the component is rerendered. You should wrap it in |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I wrote a program to demonstrate chat function by using libp2p/mdns/gossipsub.
The ui got frozen when stroking in the input box.
Anyone has an idea to fix it?
Thanks a lot!
Here is the main code.
// chat.rs
`
use dioxus::prelude::*;
use tokio::sync::mpsc;
mod network;
use network::{NetworkCommand, NetworkEvent};
#[component]
pub fn ChatRoom() -> Element {
let mut input_value = use_signal(|| String::new());
let mut messages = use_signal(|| Vec::::new());
}
`
// network.rs
`
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
use std::time::Duration;
use futures::StreamExt;
use libp2p::{
gossipsub, mdns, noise,
swarm::{NetworkBehaviour, SwarmEvent},
tcp, yamux,
};
use tokio::sync::mpsc::{Receiver, Sender};
/// 定义网络事件枚举,用于从后台任务向前端发送事件
#[derive(Debug)]
pub enum NetworkEvent {
MessageReceived(String),
PeerConnected(String),
PeerDisconnected(String),
}
/// 定义网络命令枚举,用于从前端向后台任务发送命令
#[derive(Debug)]
pub enum NetworkCommand {
SendMessage(String),
SubscribeTopic(String),
UnsubscribeTopic(String),
}
/// 自定义网络行为,结合 Gossipsub 和 Mdns
#[derive(NetworkBehaviour)]
struct MyBehaviour {
gossipsub: gossipsub::Behaviour,
mdns: mdns::tokio::Behaviour,
}
/// 启动网络任务
pub async fn start(
mut command_receiver: Receiver,
event_sender: Sender,
) -> Result<(), Box> {
let key = libp2p::identity::Keypair::generate_ed25519();
}
`
Beta Was this translation helpful? Give feedback.
All reactions