Crate socketeer

Source
Expand description

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

Structs§

Socketeer
A WebSocket client that manages the connection to a WebSocket server. The client can send and receive messages, and will transparently handle protocol messages.

Enums§

Error
Error type for the Socketeer library. This type is used to represent all possible external errors that can occur when using the Socketeer library.