typed-websocket

Crates.iotyped-websocket
lib.rstyped-websocket
version
sourcesrc
created_at2024-11-22 07:45:20.886104
updated_at2024-11-22 07:45:20.886104
descriptionsimple wrapper on top of websocket stream that enforces type
homepage
repositoryhttps://github.com/kanekoshoyu/kucoin_arbitrage
max_upload_size
id1457160
Cargo.toml error:TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Sho Kaneko (kanekoshoyu)

documentation

README

typed-websocket

crates docs license

simple wrapper on top of websocket stream that enforces type

use case

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    use tokio_tungstenite::connect_async;
    use url::Url;

    let url = Url::parse("wss://example.com/socket")?;
    let (stream, _) = connect_async(url).await?;

    // Create a TypedWebSocketStream with specific input/output types
    let mut ws: TypedWebSocketStream<_, RequestMessage, ResponseMessage> = TypedWebSocketStream::new(stream);

    // Send a message
    let outgoing = RequestMessage::new();
    ws.send(outgoing).await?;

    // Receive a message
    match ws.receive().await {
        Ok(incoming) => println!("Received: {:?}", incoming),
        Err(e) => eprintln!("Error receiving message: {}", e),
    }

    // Close the connection
    ws.close().await?;

    Ok(())
}

requirement

  • INPUT: impl Serialize
  • OUTPUT: impl Deserialize
// Define input and output message types
#[derive(Serialize, Debug)]
struct RequestMessage {
    command: String,
    data: String,
}

#[derive(Deserialize, Debug)]
struct ResponseMessage {
    response: String,
    status: u16,
}

notes

Commit count: 228

cargo fmt