Crate trillium_websockets

source ·
Expand description

§A websocket trillium handler

There are three primary ways to use this crate

§With an async function that receives a WebSocketConn

This is the simplest way to use trillium websockets, but does not provide any of the affordances that implementing the WebSocketHandler trait does. It is best for very simple websockets or for usages that require moving the WebSocketConn elsewhere in an application. The WebSocketConn is fully owned at this point, and will disconnect when dropped, not when the async function passed to websocket completes.

use futures_lite::stream::StreamExt;
use trillium_websockets::{Message, WebSocketConn, websocket};

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

§Implementing WebSocketHandler

WebSocketHandler provides support for sending outbound messages as a stream, and simplifies common patterns like executing async code on received messages.

§Using JsonWebSocketHandler

[JsonWebSocketHandler] provides a thin serialization and deserialization layer on top of WebSocketHandler for this common use case. See the [JsonWebSocketHandler] documentation for example usage. In order to use this trait, the json cargo feature must be enabled.

Re-exports§

  • pub use async_tungstenite;
  • pub use async_tungstenite::tungstenite;

Structs§

  • The trillium handler. See crate-level docs for example usage.
  • The configuration for WebSocket connection.
  • A struct that represents an specific websocket connection.

Enums§

  • An Error type that represents all exceptional conditions that can be encoutered in the operation of this crate
  • An enum representing the various forms of a WebSocket message.
  • Indicates a Client or Server role of the websocket

Traits§

  • This is the trait that defines a handler for trillium websockets.

Functions§

Type Aliases§

  • a Result type for this crate

Attribute Macros§