#tcp #transport #tcp-client #tcp-server #r-socket #rsocket #tokio

rsocket_rust_transport_tcp

TCP RSocket 传输实现

10 个版本

0.7.4 2023年6月1日
0.7.2 2021年9月1日
0.7.1 2021年5月13日
0.7.0 2021年1月14日
0.5.0 2020年2月22日

#2#rsocket

Download history 17/week @ 2024-04-01 6/week @ 2024-05-27 10/week @ 2024-06-10 9/week @ 2024-06-24 18/week @ 2024-07-01 13/week @ 2024-07-08 49/week @ 2024-07-15

89 每月下载量
用于 rsocket_rust_messaging

Apache-2.0 许可

165KB
4.5K SLoC

RSocket TCP 传输

示例

在您的 Cargo.toml 中添加依赖项。

[dependencies]
tokio = "0.3.6"
rsocket_rust = "0.7.0"
rsocket_rust_transport_tcp = "0.7.0"

服务器

use log::info;
use rsocket_rust::prelude::{RSocketFactory, ServerResponder};
use rsocket_rust::Result;
use rsocket_rust::utils::EchoRSocket;
use rsocket_rust_transport_tcp::TcpServerTransport;

#[tokio::main]
async fn main() -> Result<()> {
    let transport: TcpServerTransport = TcpServerTransport::from("127.0.0.1:7878");

    let responder: ServerResponder = Box::new(|setup, _socket| {
        info!("accept setup: {:?}", setup);
        Ok(Box::new(EchoRSocket))
        // Or you can reject setup
        // Err(From::from("SETUP_NOT_ALLOW"))
    });

    let on_start: Box<dyn FnMut() + Send + Sync> =
        Box::new(|| info!("+++++++ echo server started! +++++++"));

    RSocketFactory::receive()
        .transport(transport)
        .acceptor(responder)
        .on_start(on_start)
        .serve()
        .await?;

    Ok(())
}

客户端

use log::info;
use rsocket_rust::prelude::{ClientResponder, Payload, RSocket, RSocketFactory};
use rsocket_rust::Result;
use rsocket_rust_transport_tcp::TcpClientTransport;

#[tokio::main]
async fn main() -> Result<()> {
    let client = RSocketFactory::connect()
        .transport(TcpClientTransport::from("127.0.0.1:7878"))
        .setup(Payload::from("READY!"))
        .mime_type("text/plain", "text/plain")
        .start()
        .await?;

    let request_payload: Payload = Payload::builder()
        .set_data_utf8("Hello World!")
        .set_metadata_utf8("Rust")
        .build();

    let res = client.request_response(request_payload).await?;

    info!("got: {:?}", res);

    client.close();

    Ok(())
}

依赖项

~5–17MB
~225K SLoC