Path: redb-route/redb.Route.WebSocket/WsEndpointOptions.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.WebSocket/WsEndpointOptions.cs
----
using redb.Route.Core;
namespace redb.Route.WebSocket;
///
/// Options for the WebSocket endpoint. Shared by both producer and consumer.
/// URI format: ws:host:port/path?messageType=Text&subProtocol=graphql-ws
///
public class WsEndpointOptions : EndpointOptions
{
// ── Connection ──────────────────────────────────────
/// Host to connect or bind. Parsed from URI path.
public string Host { get; set; } = "0.0.0.0";
/// Port number. Parsed from URI path.
public int Port { get; set; } = 8080;
// ── Framing ─────────────────────────────────────────
/// Default message type for outgoing frames. Default: Text.
public WsMessageType MessageType { get; set; } = WsMessageType.Text;
/// Text encoding name. Default: utf-8.
public string Encoding { get; set; } = "utf-8";
// ── Protocol ────────────────────────────────────────
///
/// WebSocket subprotocol to negotiate (e.g. "graphql-ws", "mqtt").
/// Default: null (no subprotocol).
///
public string? SubProtocol { get; set; }
// ── Socket ──────────────────────────────────────────
/// Receive buffer size in bytes. Default: 8192.
public int ReceiveBufferSize { get; set; } = 8192;
/// Send buffer size in bytes (ClientWebSocket only). Default: 8192.
public int SendBufferSize { get; set; } = 8192;
/// Keep-alive ping interval in milliseconds. 0 = disabled. Default: 30000 (30s).
public int KeepAliveInterval { get; set; } = 30_000;
// ── Producer (client) ───────────────────────────────
/// Connect timeout in milliseconds. Default: 10000 (10s). 0 = infinite.
public int ConnectTimeout { get; set; } = 10_000;
/// Auto-reconnect on disconnect. Default: false.
public bool Reconnect { get; set; }
/// Interval between reconnect attempts in milliseconds. Default: 5000.
public int ReconnectInterval { get; set; } = 5_000;
/// Maximum reconnect attempts. 0 = unlimited. Default: 0.
public int MaxReconnectAttempts { get; set; }
///
/// Overall time budget for reconnecting, in milliseconds. 0 = no budget (default, the
/// historical behaviour). With reconnect=true, maxReconnectAttempts=0 and a
/// server that stays down, an exchange would otherwise never come back and dead-letter would
/// never fire; set this to make the send fail instead of hanging.
///
public int ReconnectTimeout { get; set; }
///
/// Producer mode. Client (default) connects to a remote server with
/// . Server pushes into the clients
/// of the consumer running on the same URI, so a route can send unsolicited frames
/// (quotes, notifications, progress) instead of only answering incoming ones.
///
public WsMode Mode { get; set; } = WsMode.Client;
// ── Consumer (server) ───────────────────────────────
/// Maximum concurrent WebSocket connections. 0 = unlimited. Default: 0.
public int MaxConnections { get; set; }
///
/// If true, the consumer returns the exchange Out body as a WebSocket response frame (InOut pattern).
/// Default: false.
///
public bool InOut { get; set; }
// ── TLS ─────────────────────────────────────────────
/// Enable TLS (wss). Default: false.
public bool Ssl { get; set; }
/// Path to PFX certificate file for TLS (consumer/server). Required when ssl=true for consumer.
public string? SslCertPath { get; set; }
/// Password for the PFX certificate.
[Sensitive]
public string? SslCertPassword { get; set; }
///
/// Producer only: accept any server certificate on a wss:// connection, including a
/// self-signed one. Off by default and never implied by another option — turning certificate
/// validation off has to be an explicit decision, and it belongs to staging, not production.
///
public bool TrustAllCertificates { get; set; }
///
/// Named from the route registry. Lets the TLS certificate
/// password live in the registry instead of the endpoint URI, so it never reaches logs
/// or dashboards.
///
[RegistryReference(typeof(WsConnectionFactory))]
public string? ConnectionFactory { get; set; }
///
public override void Validate()
{
if (Port is < 0 or > 65535)
throw new ArgumentException("Port must be between 0 and 65535.");
if (ReceiveBufferSize <= 0)
throw new ArgumentException("ReceiveBufferSize must be > 0.");
if (SendBufferSize <= 0)
throw new ArgumentException("SendBufferSize must be > 0.");
if (KeepAliveInterval < 0)
throw new ArgumentException("KeepAliveInterval must be >= 0.");
if (ConnectTimeout < 0)
throw new ArgumentException("ConnectTimeout must be >= 0.");
if (ReconnectInterval <= 0)
throw new ArgumentException("ReconnectInterval must be > 0.");
if (MaxReconnectAttempts < 0)
throw new ArgumentException("MaxReconnectAttempts must be >= 0.");
if (ReconnectTimeout < 0)
throw new ArgumentException("ReconnectTimeout must be >= 0.");
if (MaxConnections < 0)
throw new ArgumentException("MaxConnections must be >= 0.");
// Without this the mistake surfaces as a bare framework ArgumentException from the
// consumer's constructor, with no hint which option or which endpoint is at fault.
try
{
System.Text.Encoding.GetEncoding(Encoding);
}
catch (ArgumentException ex)
{
throw new ArgumentException(
$"Unknown encoding '{Encoding}'. Use a name System.Text.Encoding knows, such as utf-8.", ex);
}
}
}
/// Producer mode for the WebSocket component.
public enum WsMode
{
/// Connect to a remote WebSocket server and send frames on it.
Client,
/// Push frames into the clients of the local consumer serving the same URI.
Server,
}