Crate trillium_websockets

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§

JsonHandler
A wrapper type for JsonWebSocketHandlers
WebSocket
The trillium handler. See crate-level docs for example usage.
WebSocketConfig
The configuration for WebSocket connection.
WebSocketConn
A struct that represents an specific websocket connection.

Enums§

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

Traits§

JsonWebSocketHandler
Implement this trait to use websockets with a json handler
WebSocketHandler
This is the trait that defines a handler for trillium websockets.

Functions§

json_websocket
builds a new trillium handler from the provided JsonWebSocketHandler. Alias for WebSocket::new_json
websocket
Builds a new trillium handler from the provided WebSocketHandler. Alias for WebSocket::new
websocket_accept_hash
Generate the expected Sec-WebSocket-Accept hash from the Sec-WebSocket-Key
websocket_key
Generate a random key suitable for Sec-WebSocket-Key

Type Aliases§

Result
a Result type for this crate

Attribute Macros§

async_trait