Rust

The network adapter on tokio and tokio-tungstenite, and the client's network transport.

Contents

use utopia_websocket::net::Config;
Rust
pub struct Config

The configuration a Tokio server starts with.

use utopia_websocket::Tokio;
Rust
pub struct Tokio

A WebSocket server on the network, on tokio.

start listens on the host and port, calls the start hook and the worker start hook for each worker, then serves until shutdown. Each connection gets the next id from 1. A request that asks for a WebSocket upgrade becomes a connection (the open hook gets its handshake); any other request goes to the request hook and is answered with the response it fills, then the connection is closed.

send queues the message for each connection and returns at once; each connection writes its queue in order. A write that waits longer than the send timeout for the peer to read resets the connection (its queued messages are dropped and the close hook runs), so a stalled client cannot hold memory or block others. A message is sent as a text frame, or as a binary frame when it is not UTF-8.

close writes the connection's queued messages, then ends its TCP connection without a close frame: the close code is not sent, and the client sees the connection end.

Rust
use utopia_websocket::{Server, Tokio};

let adapter = Tokio::new("0.0.0.0", 8080, 5.0)?;
let server = Server::new(adapter);
let replies = server.clone();
server
    .on_open(|connection, request| println!("{connection} opened {}", request.uri()))
    .on_message(move |connection, message| replies.send(&[connection], message.to_vec()))
    .error(|error, stage| eprintln!("{stage}: {error}"));
server.start().await;
Compiles in CI; needs a running service to execute.
Rust
pub const DEFAULT_SEND_TIMEOUT: f64 = 5.0

The send timeout when none is given: 5 seconds.