#websocket #transport #tokio #cargo-toml

rsocket_rust_transport_websocket

Websocket RSocket transport implementation

9 releases

0.7.3 Dec 12, 2021
0.7.2 Sep 1, 2021
0.7.1 May 13, 2021
0.7.0 Jan 14, 2021
0.5.0 Feb 22, 2020

#97 in WebSocket

Download history 11/week @ 2022-03-01 7/week @ 2022-03-08 32/week @ 2022-03-15 33/week @ 2022-03-22 11/week @ 2022-03-29 6/week @ 2022-04-05 6/week @ 2022-04-12 10/week @ 2022-04-19 74/week @ 2022-04-26 71/week @ 2022-05-03 140/week @ 2022-05-10 34/week @ 2022-05-17 50/week @ 2022-05-24 88/week @ 2022-05-31 27/week @ 2022-06-07 5/week @ 2022-06-14

171 downloads per month
Used in rsocket_rust_messaging

Apache-2.0

160KB
4.5K SLoC

RSocket Transport For Websocket

Example

Add dependencies in your Cargo.toml.

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

Server

use log::info;
use rsocket_rust::prelude::*;
use rsocket_rust::utils::EchoRSocket;
use rsocket_rust::Result;
use rsocket_rust_transport_websocket::WebsocketServerTransport;

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

    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(())
}

Client

use log::info;
use rsocket_rust::prelude::*;
use rsocket_rust::Result;
use rsocket_rust_transport_websocket::WebsocketClientTransport;

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

    let request_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(())
}

Dependencies

~7–15MB
~273K SLoC