Crates.io | connection |
lib.rs | connection |
version | 0.2.5 |
source | src |
created_at | 2023-03-26 23:24:36.92342 |
updated_at | 2023-03-31 06:41:36.125264 |
description | A TCP connection that can read and write serializable data |
homepage | |
repository | |
max_upload_size | |
id | 821560 |
size | 13,721 |
A TCP-based connection that can send & receive serializable objects.
Add this to your Cargo.toml:
[dependencies]
connection = "0.2.5"
You can create a Connection
by connecting like so:
use connection::Connection;
#[tokio::main]
async fn main() {
let mut conn = Connection::connect("127.0.0.1:8080").await.unwrap();
}
You can use the Connection
to send and receive serializable objects:
use connection::Connection;
use serde::{Serialize, Deserialize};
/// A (de)serializable type shared between client and server
#[derive(Serialize, Deserialize)]
struct Message {
id: u32,
data: String,
}
/// Code running client side
async fn client_side(mut client_conn: Connection) {
let message = Message {
id: 1,
data: "Hello, world!".to_string(),
};
client_conn.write::<Message>(&message).await.unwrap();
}
/// Code running server side
async fn server_side(mut server_conn: Connection) {
let message: Message = server_conn.read::<Message>().await.unwrap().unwrap();
}