#websocket-client #ws #server

web-socket

Fastest webSocket implementation for both client and server

4 releases (2 breaking)

0.7.0 May 7, 2023
0.6.0 Mar 30, 2023
0.5.1 Mar 28, 2023
0.4.2 Mar 8, 2023
0.1.0 Oct 28, 2022

#330 in WebSocket

Download history 117/week @ 2024-07-29 25/week @ 2024-08-05 7/week @ 2024-08-12 26/week @ 2024-08-19 7/week @ 2024-08-26 47/week @ 2024-09-02 11/week @ 2024-09-09 4/week @ 2024-09-23 39/week @ 2024-09-30 25/week @ 2024-10-07 111/week @ 2024-10-14 58/week @ 2024-10-21 27/week @ 2024-10-28 27/week @ 2024-11-04

227 downloads per month
Used in 2 crates (via web-socket-io)

Apache-2.0

27KB
466 lines

Introduction

This library is an implementation of the WebSocket protocol, which provides a way for two-way communication between a client and server over a single TCP connection. This library provides fastest and intuitive WebSocket implementation for both client and server-side applications.

Installation

To use this library, add it as a dependency to your Rust project by adding the following line to your Cargo.toml file:

[dependencies]
web-socket = "0.7"

Example

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

use tokio::io::*;
use web_socket::*;

async fn example<IO>(mut ws: WebSocket<IO>) -> Result<()>
where
    IO: Unpin + AsyncRead + AsyncWrite,
{
    for _ in 0..3 {
        ws.send("Copy Cat!").await?;

        match ws.recv_event().await? {
            Event::Data { ty, data } => {
                assert!(matches!(ty, DataType::Complete(MessageType::Text)));
                assert_eq!(&*data, b"Copy Cat!");
            }
            Event::Ping(data) => ws.send_pong(data).await?,
            Event::Pong(..) => {}
            Event::Error(..) => return ws.close(CloseCode::ProtocolError).await,
            Event::Close { .. } => return ws.close(()).await,
        }
    }
    ws.close("bye!").await
}

For more examples, see ./examples directory.

It passed all test of the autobahn testsuite

License

This project is licensed under Apache License 2.0

Dependencies

~2.4–8MB
~55K SLoC