#ws #websocket

web-socket

WebSocket implementation for both client and server

7 unstable releases (3 breaking)

0.4.2 Mar 8, 2023
0.4.1 Mar 8, 2023
0.3.0 Mar 6, 2023
0.2.1 Feb 8, 2023
0.1.0 Oct 28, 2022

#44 in WebSocket

Download history 6/week @ 2022-11-22 3/week @ 2022-12-06 3/week @ 2022-12-13 1/week @ 2022-12-27 3/week @ 2023-01-10 2/week @ 2023-01-17 4/week @ 2023-01-24 4/week @ 2023-01-31 29/week @ 2023-02-07 9/week @ 2023-02-14 3/week @ 2023-02-21 19/week @ 2023-02-28 72/week @ 2023-03-07

103 downloads per month

Apache-2.0

55KB
964 lines

Web-Socket

This library provide WebSocket implementation for both client and server.

Usage

Run:

cargo add web-socket

Or add this to your Cargo.toml file.

[dependencies]
web-socket = "0.3"

Ping-Pong Example

You can run this example with: cargo run --example ping_pong

use std::io::Result;
use web_socket::{client::WSS, DataType, Event};

async fn example() -> Result<()> {
    let mut ws = WSS::connect("ws.ifelse.io:443", "/").await?;

    ws.on_event = Box::new(|ev| {
        if let Event::Pong(_) = ev {
            println!("Pong: {}", ev.to_string());
        }
        Ok(())
    });

    for _ in 0..5 {
        ws.send(Event::Ping(b"Hello!")).await?;
        ws.send("Copy Cat!").await?;

        let mut data = ws.recv().await?;
        assert_eq!(data.ty, DataType::Text);

        let mut buf = vec![];
        data.read_to_end(&mut buf).await?;
        println!("Text: {:?}", String::from_utf8(buf));
    }
    Ok(())
}

For more examples, see ./examples directory.

It passed every test of the autobahn testsuite

License

This project is licensed under Apache License 2.0

Dependencies

~2.4–9MB
~151K SLoC