4 个版本

0.1.3 2023 年 9 月 19 日
0.1.2 2023 年 1 月 2 日
0.1.1 2022 年 11 月 29 日
0.1.0 2022 年 11 月 11 日

#3#mezzenger

Apache-2.0

25KB
491 行代码(不包括注释)

mezzenger-webworker

用于与 Web Workers 通信的传输。

https://crates.io/crates/mezzenger-webworker

ko-fi

用法

将依赖项添加到 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"

在主代码中

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");

在 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");

更多示例请参阅 rust-webapp-template

另请参阅

mezzenger

rust-webapp-template

使用 Web Workers

Web Worker 中的 WASM

依赖项

~7.5–10MB
~191K SLoC