Crate trillium_websockets[][src]

Expand description

A websocket trillium handler

use trillium_websockets::{Message, WebSocket};

let handler = WebSocket::new(|mut websocket| async move {
    let path = websocket.path().to_owned();
    while let Some(Ok(Message::Text(input))) = websocket.next().await {
        websocket
            .send_string(format!("received your message: {} at path {}", &input, path))
            .await;
    }
});

trillium_testing::with_server(handler, |url| async move {
    let socket = TcpStream::connect(&url.socket_addrs(|| None)?[..]).await?;
    let (mut client, _) = async_tungstenite::client_async("ws://localhost/some/route", socket).await?;

    client.send(Message::text("hello")).await?;
    let received_message = client.next().await.unwrap()?.into_text()?;
    assert_eq!("received your message: hello at path /some/route", received_message);

    client.send(Message::text("hey")).await?;
    let received_message = client.next().await.unwrap()?.into_text()?;
    assert_eq!("received your message: hey at path /some/route", received_message);

    Ok(())
});

Re-exports

pub use async_tungstenite::tungstenite;

Structs

The trillium handler. See crate-level docs for example usage.

A struct that represents an specific websocket connection.

Enums

Possible WebSocket errors.

An enum representing the various forms of a WebSocket message.

Type Definitions

a Result type for websocket messages