4 releases
0.1.3 | Sep 19, 2023 |
---|---|
0.1.2 | Jan 2, 2023 |
0.1.1 | Nov 29, 2022 |
0.1.0 | Nov 11, 2022 |
#5 in #mezzenger
25KB
491 lines
mezzenger-webworker
Transport for communication with Web Workers.
https://crates.io/crates/mezzenger-webworker
usage
Add dependencies to Cargo.toml
:
[dependencies]
# ...
serde = { version = "1", features = ["derive"] }
kodec = { version = "0.1.0", features = ["binary"] } # or json or different one from another crate...
mezzenger = "0.1.3"
mezzenger-webworker = "0.1.0"
In your main code:
mod message {
#[derive(Debug, Serialize, Deserialize)]
struct Host {
...
}
#[derive(Debug, Serialize, Deserialize)]
struct Worker {
...
}
}
// ...
let worker = Rc::new(Worker::new("./worker.js").unwrap());
let mut transport: Transport<_, Codec, message::Worker, message::Host> =
Transport::new(&worker, Codec::default()).await.except("failed to open transport");
use mezzenger::Receive;
let received = transport.receive().await.except("failed to receive message");
let message = message::Host { ... };
transport.send(&message).await.except("failed to send message");
In Web Worker:
mod message {
#[derive(Debug, Serialize, Deserialize)]
struct Host {
...
}
#[derive(Debug, Serialize, Deserialize)]
struct Worker {
...
}
}
// ...
let mut transport: Transport<_, Codec, message::Host, message::Worker> =
Transport::new_in_worker(Codec::default()).await.except("failed to open transport");
let message = message::Worker { ... };
transport.send(&message).await.except("failed to send message");
use mezzenger::Receive;
let received = transport.receive().await.except("failed to receive message");
See rust-webapp-template for more comprehensive example.
see also
Dependencies
~8–11MB
~203K SLoC