104 个重大版本发布

0.114.0 2024 年 8 月 14 日
0.112.0 2024 年 8 月 6 日
0.110.0 2024 年 7 月 3 日
0.99.0 2024 年 3 月 19 日
0.10.0 2021 年 7 月 29 日

#699密码学

Download history 159/week @ 2024-04-29 137/week @ 2024-05-06 12/week @ 2024-05-20 295/week @ 2024-05-27 11/week @ 2024-06-03 153/week @ 2024-06-10 8/week @ 2024-06-17 176/week @ 2024-06-24 107/week @ 2024-07-01 123/week @ 2024-08-05 275/week @ 2024-08-12

398 每月下载量

Apache-2.0

695KB
15K SLoC

ockam_transport_websocket

crate docs license discuss

Ockam 是一个用于构建与云服务和其它设备安全、私密且可信赖通信的设备的库。

该包为 Ockam 路由协议提供了 WebSocket 传输。

此包需要 rust 标准库 "std"

我们需要定义将处理传入消息的工作者的行为。

use ockam_core::{Worker, Result, Routed, async_trait};
use ockam_node::Context;

struct MyWorker;

#[async_trait]
impl Worker for MyWorker {
    type Context = Context;
    type Message = String;

    async fn handle_message(&mut self, _ctx: &mut Context, _msg: Routed<String>) -> Result<()> {
        // ...
        Ok(())
    }
}

// Now we can write the main function that will run the previous worker. In this case, our worker will be listening for new connections on port 8000 until the process is manually killed.

use ockam_transport_websocket::WebSocketTransport;
use ockam_node::NodeBuilder;
use ockam_macros::node;

#[ockam_macros::node(crate = "ockam_node")]
async fn main(mut ctx: Context) -> Result<()> {//!
    let ws = WebSocketTransport::create(&ctx).await?;
    ws.listen("localhost:8000").await?; // Listen on port 8000

    // Start a worker, of type MyWorker, at address "my_worker"
    ctx.start_worker("my_worker", MyWorker).await?;

    // Run worker indefinitely in the background
    Ok(())
}

最后,我们可以编写另一个节点连接到托管 MyWorker 工作者的节点,我们就可以在它们之间发送和接收消息了。

use ockam_transport_websocket::{WebSocketTransport, WS};
use ockam_core::{route, Result};
use ockam_node::Context;
use ockam_macros::node;

#[ockam_macros::node(crate = "ockam_node")]
async fn main(mut ctx: Context) -> Result<()> {
    use ockam_node::MessageReceiveOptions;
let ws = WebSocketTransport::create(&ctx).await?;

    // Define the route to the server's worker.
    let r = route![(WS, "localhost:8000"), "my_worker"];

    // Now you can send messages to the worker.
    ctx.send(r, "Hello Ockam!".to_string()).await?;

    // Or receive messages from the server.
    let reply = ctx.receive::<String>().await?;

    // Stop all workers, stop the node, cleanup and return.
    ctx.stop().await
}

用法

将此添加到您的 Cargo.toml

[dependencies]
ockam_transport_websocket = "0.114.0"

许可证

此代码根据 Apache License 2.0 许可证授权。

依赖项

~4–15MB
~183K SLoC