Skip to content

Latest commit

 

History

History
42 lines (36 loc) · 1.72 KB

README.md

File metadata and controls

42 lines (36 loc) · 1.72 KB

socketeer

Crates.io Version docs.rs GitHub branch status Codecov

socketeer is a small wrapper which creates a tokio-tungstenite websocket connection and exposes a simplified async API to send and receive application messages. It automatically handles the underlying websocket and allows for reconnection, as well as immediate handling of errors in client code. Currently supports only json encoded binary and text messages.

Usage

use socketeer::Socketeer;

#[derive(Debug, serde::Serialize, serde::Deserialize)]
struct SocketMessage {
    message: String,
}

#[tokio::main]
async fn main() {
    // Create a Socketeer instance that connects to a fictional local server.
    let mut socketeer: Socketeer<SocketMessage, SocketMessage> =
        Socketeer::connect("ws://127.0.0.1:80")
            .await
            .unwrap();
    // Send a message to the server
    socketeer
        .send(SocketMessage {
            message: "Hello, world!".to_string(),
        })
        .await
        .unwrap();
    // Wait for the server to respond
    let response = socketeer.next_message().await.unwrap();
    println!("{response:#?}");
    // Shut everything down and close the connection
    socketeer.close_connection().await.unwrap();
}