Here are some example codes which show how RSocket works in Rust.
Add dependencies in your Cargo.toml
.
```toml [dependencies] tokio = "0.2" rsocket_rust = "*"
```
```rust
extern crate log;
use rsocketrust::prelude::*; use rsocketrusttransporttcp::TcpServerTransport; use std::env; use std::error::Error;
async fn main() -> Result<(), Box
RSocketFactory::receive()
.transport(TcpServerTransport::from(addr))
.acceptor(|setup, _socket| {
info!("accept setup: {:?}", setup);
Ok(Box::new(EchoRSocket))
// Or you can reject setup
// Err(From::from("SETUP_NOT_ALLOW"))
})
.on_start(|| info!("+++++++ echo server started! +++++++"))
.serve()
.await
} ```
```rust use rsocketrust::prelude::*; use rsocketrusttransporttcp::TcpClientTransport;
async fn test() { let cli = RSocketFactory::connect() .acceptor(|| Box::new(EchoRSocket)) .transport(TcpClientTransport::from("127.0.0.1:7878")) .setup(Payload::from("READY!")) .mimetype("text/plain", "text/plain") .start() .await .unwrap(); let req = Payload::builder() .setdatautf8("Hello World!") .setmetadatautf8("Rust") .build(); let res = cli.requestresponse(req).await.unwrap(); println!("got: {:?}", res); cli.close(); }
```