Rust

adapter

Module

The interface every server adapter implements, and the hooks it calls.

Contents

use utopia_websocket::Adapter;
Rust
pub trait Adapter: Send + Sync + 'static

A WebSocket server implementation: Tokio on the network, Memory in process. Server drives one.

Adapters are cheap handles (clones share one server), so a hook can keep one to send from inside an event. Hooks are registered before start; each on_* method replaces the hook set before.

Implemented byMemoryTokio
Rust
pub type CloseHook = Arc<dyn Fn(ConnectionId) + Send + Sync>

Called when a connection is closed, by either side.

Rust
pub type ConnectionId = i64

The id of one WebSocket connection, unique while it is open.

Rust
pub type MessageHook = Arc<dyn Fn(ConnectionId, &[u8]) + Send + Sync>

Called with each message a connection sends.

Rust
pub type OpenHook = Arc<dyn Fn(ConnectionId, &Request) + Send + Sync>

Called when a connection is established, with its handshake request.

Rust
pub type Request = http::Request<Bytes>

An HTTP request: the handshake of a WebSocket connection (to Adapter::on_open) or a plain request on the server's port (to Adapter::on_request).

Rust
pub type RequestHook = Arc<dyn Fn(&Request, &mut Response) + Send + Sync>

Called with each plain HTTP request and the response to fill.

Rust
pub type Response = http::Response<Vec<u8>>

The response an Adapter::on_request hook fills: 200 OK with no headers and an empty body until the hook changes it.

Rust
pub type StartHook = Arc<dyn Fn() + Send + Sync>

Called once when the server starts listening.

Rust
pub type WorkerHook = Arc<dyn Fn(i64) + Send + Sync>

Called with a worker id when a worker starts or stops.