18 releases

0.8.1 Dec 2, 2024
0.8.0 Mar 8, 2024
0.7.1 Nov 8, 2021
0.6.0 Jun 15, 2021
0.2.2 Jun 25, 2019

#6 in WebSocket

Download history 139068/week @ 2024-08-20 160320/week @ 2024-08-27 172843/week @ 2024-09-03 160568/week @ 2024-09-10 145257/week @ 2024-09-17 152341/week @ 2024-09-24 164769/week @ 2024-10-01 170199/week @ 2024-10-08 179669/week @ 2024-10-15 179271/week @ 2024-10-22 163143/week @ 2024-10-29 172953/week @ 2024-11-05 151026/week @ 2024-11-12 145809/week @ 2024-11-19 144164/week @ 2024-11-26 131279/week @ 2024-12-03

601,665 downloads per month
Used in 483 crates (25 directly)

Apache-2.0 OR MIT

105KB
2.5K 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 soketto::handshake::{Client, ServerResponse};

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

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

// 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 mut data = Vec::new();
receiver.receive_data(&mut data).await?;


Server example

use soketto::{handshake::{Server, ClientRequest, server::Response}};

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

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

    let websocket_key = {
        let req = server.receive_request().await?;
        req.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 mut data = Vec::new();
    let data_type = receiver.receive_data(&mut data).await?;

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

    sender.close().await?
}


See examples/hyper_server.rs from this crate's repository for an example of starting up a WebSocket server alongside an Hyper HTTP server.

Dependencies

~3–4.5MB
~86K SLoC