-
Notifications
You must be signed in to change notification settings - Fork 28
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Failing to run web demo #36
Comments
Does it work when using |
just to be sure, is there something different from adding directly into an eframe_template + |
I got the same problem with the example once adding my code.. not sure exactly how. use eframe::wasm_bindgen::{self, prelude::*};
/// This is the entry-point for all the web-assembly.
/// This is called once from the HTML.
/// It loads the app, installs some callbacks, then returns.
/// You can add more callbacks like this if you want to call in to your code.
#[wasm_bindgen]
pub async fn start(canvas_id: &str) -> Result<(), JsValue> {
// Redirect `log` message to `console.log` and friends:
eframe::WebLogger::init(log::LevelFilter::Debug).ok();
// let app = crate::ExampleApp::default();
let app = App::default().await;
eframe::WebRunner::new()
.start(canvas_id, Default::default(), Box::new(|_cc| Box::new(app)))
.await?;
Ok(())
}
use chrono::prelude::*;
use humantime::format_duration;
//use tokio_with_wasm::runtime::Runtime;
use tokio_with_wasm::tokio_wasm::sync::mpsc;
use futures_util::{SinkExt, StreamExt};
use tokio_tungstenite_wasm::connect;
use url::Url;
use std::collections::BTreeMap;
struct MessageInfo {
value: serde_json::Value,
previous_value: Option<serde_json::Value>,
last_sample_time: DateTime<chrono::Utc>,
}
impl MessageInfo {
pub fn update(&mut self, value: serde_json::Value) {
}
}
struct App {
receiver: mpsc::Receiver<String>,
vehicles: BTreeMap<u8, BTreeMap<u8, BTreeMap<String, MessageInfo>>>,
}
impl App {
async fn default() -> Self {
let (tx, rx) = mpsc::channel(32);
wasm_bindgen_futures::spawn_local(connect_and_receive_messages(tx));
Self {
receiver: rx,
vehicles: Default::default(),
}
}
}
async fn connect_and_receive_messages(mut tx: mpsc::Sender<String>) {
let ws =
ewebsock::connect("ws://0.0.0.0:8088/ws/mavlink", ewebsock::Options {
max_incoming_frame_size: 100,
}).expect("Can't connect");
let (mut sende, receiver) = ws;
while let Some(ewebsock::WsEvent::Message(message)) = receiver.try_recv() {
if let ewebsock::WsMessage::Text(message) = message {
eframe::web_sys::console::log_1(&message.to_string().into());
tx.send(message)
.await
.unwrap();
}
}
}
impl eframe::App for App {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ctx.request_repaint();
while let Ok(message) = self.receiver.try_recv() {
let Ok(message) = serde_json::from_str::<serde_json::Value>(&message) else {
continue;
};
let system_id: u8 = message["header"]["system_id"].as_u64().unwrap() as u8;
let component_id: u8 = message["header"]["component_id"].as_u64().unwrap() as u8;
let message_name: String = message["message"]["type"].to_string().trim_matches(['"']).to_string();
if !self.vehicles.contains_key(&system_id) {
self.vehicles.insert(system_id, Default::default());
}
if !self.vehicles[&system_id].contains_key(&component_id) {
self.vehicles.get_mut(&system_id).unwrap().insert(component_id, Default::default());
}
let previous_message = match self.vehicles.get(&system_id).unwrap().get(&component_id).unwrap().get(&message_name) {
Some(previous_message) => Some(previous_message.value.clone()),
None => None,
};
self.vehicles.get_mut(&system_id).unwrap().get_mut(&component_id).unwrap().insert(message_name, MessageInfo {
value: message,
previous_value: previous_message,
last_sample_time: Utc::now(),
});
}
for (system_id, components) in &self.vehicles {
egui::CollapsingHeader::new(format!("Vehicle {system_id}")).default_open(true).show(ui, |ui| {
for (component_id, messages) in components {
egui::CollapsingHeader::new(format!("Component {component_id}")).default_open(true).show(ui, |ui| {
for (name, message) in messages {
ui.collapsing(name, |ui| {
ui.label(serde_json::to_string_pretty(&message.value).unwrap());
ui.label(format_duration((Utc::now() - message.last_sample_time).to_std().unwrap()).to_string() + " Ago");
});
}
});
}
});
}
});
}
} |
check your wasm bindgen version? 🤷 |
Here are my dependenices:
|
The text was updated successfully, but these errors were encountered: