Crates.io | mezzenger-webworker |
lib.rs | mezzenger-webworker |
version | 0.1.3 |
source | src |
created_at | 2022-11-11 14:44:22.977715 |
updated_at | 2023-09-19 12:52:43.315668 |
description | Transport for communicating with web workers. |
homepage | https://github.com/zduny/mezzenger/tree/master/mezzenger-webworker |
repository | https://github.com/zduny/mezzenger |
max_upload_size | |
id | 712886 |
size | 28,014 |
Transport for communication with Web Workers.
https://crates.io/crates/mezzenger-webworker
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.