9 releases

✓ Uses Rust 2018 edition

new 0.3.2 Jan 22, 2020
0.3.1 Dec 30, 2019
0.2.3 Aug 19, 2019
0.2.2 Jun 25, 2019
0.1.0 Jun 5, 2019

#6 in WebSocket

Download history 502/week @ 2019-10-13 594/week @ 2019-10-20 529/week @ 2019-10-27 723/week @ 2019-11-03 1056/week @ 2019-11-10 1277/week @ 2019-11-17 1289/week @ 2019-11-24 1903/week @ 2019-12-01 1891/week @ 2019-12-08 1490/week @ 2019-12-15 536/week @ 2019-12-22 849/week @ 2019-12-29 2458/week @ 2020-01-05 2569/week @ 2020-01-12 2205/week @ 2020-01-19

5,974 downloads per month
Used in 3 crates (via libp2p-websocket)

Apache-2.0 OR MIT

100KB
2K SLoC

Soketto

An implementation of the RFC 6455 websocket protocol. This crate is a heavily modified fork of the twist crate.


lib.rs:

An implementation of the RFC 6455 websocket protocol.

To begin a websocket connection one first needs to perform a handshake, either as client or server, in order to upgrade from HTTP. Once successful, the client or server can transition to a connection, i.e. a Sender/Receiver pair and send and receive textual or binary data.

Note: While it is possible to only receive websocket messages it is not possible to only send websocket messages. Receiving data is required in order to react to control frames such as PING or CLOSE. While those will be answered transparently they have to be received in the first place, so calling [connection::Receiver::receive] is imperative.

Note: None of the async methods are safe to cancel so their Futures must not be dropped unless they return Poll::Ready.

Client example

# use async_std::net::TcpStream;
# let _: Result<(), soketto::BoxedError> = async_std::task::block_on(async {
use soketto::handshake::{Client, ServerResponse};

// First, we need to establish a TCP connection.
let socket = TcpStream::connect("...").await?;

// Then we configure the client handshake.
let mut client = Client::new(socket, "...", "/");

// And finally we perform the handshake and handle the result.
let (mut sender, mut receiver) = match client.handshake().await? {
    ServerResponse::Accepted { .. } => client.into_builder().finish(),
    ServerResponse::Redirect { status_code, location } => unimplemented!("follow location URL"),
    ServerResponse::Rejected { status_code } => unimplemented!("handle failure")
};

// Over the established websocket connection we can send
sender.send_text("some text").await?;
sender.send_text("some more text").await?;
sender.flush().await?;

// ... and receive data.
let data = receiver.receive_data().await?;

# Ok(())
# });

Server example

# use async_std::{net::TcpListener, prelude::*};
# let _: Result<(), soketto::BoxedError> = async_std::task::block_on(async {
use soketto::{handshake::{Server, ClientRequest, server::Response}};

// First, we listen for incoming connections.
let listener = TcpListener::bind("...").await?;
let mut incoming = listener.incoming();

while let Some(socket) = incoming.next().await {
    // For each incoming connection we perform a handshake.
    let mut server = Server::new(socket?);

    let websocket_key = {
        let req = server.receive_request().await?;
        req.into_key()
    };

    // Here we accept the client unconditionally.
    let accept = Response::Accept { key: &websocket_key, protocol: None };
    server.send_response(&accept).await?;

    // And we can finally transition to a websocket connection.
    let (mut sender, mut receiver) = server.into_builder().finish();

    let data = receiver.receive_data().await?;

    if data.is_text() {
        sender.send_text(std::str::from_utf8(data.as_ref())?).await?
    } else {
        sender.send_binary(data.as_ref()).await?
    }

    sender.close().await?;
}

# Ok(())
# });

Dependencies

~2.6–3.5MB
~68K SLoC