Hcs.Websocket
hcs · API reference
WebSocket upgrade and frame I/O support.
Use this module when an HTTP route upgrades to a WebSocket and you want to send or receive text, binary, ping/pong, and close frames directly. HCS does not impose an application channel protocol; build that protocol in your app or combine WebSockets with Hive workers/pubsub.
Constants
val websocket_uuid : stringUUID used in WebSocket handshake per RFC 6455
Types
module Opcode : sig ... endWebSocket frame opcode
type frame = {
opcode : Opcode.t;
extension : int;
final : bool;
content : string;
}WebSocket frame
val pp_frame : Format.formatter -> frame -> unitval make_frame :
?opcode:Opcode.t ->
?extension:int ->
?final:bool ->
?content:string ->
unit ->
frameval close_frame : int -> frameval default_max_payload_size : inttype t = {
flow : Eio.Flow.two_way_ty Eio.Std.r;
mutable closed : bool;
is_client : bool;
read_buf : Buffer.t;
max_payload_size : int;
}type error =
| Connection_closed
| Protocol_error of string
| Io_error of string
| Payload_too_large of intCryptographic helpers
val b64_encoded_sha1sum : String.t -> stringCompute SHA-1 hash and base64 encode
val compute_accept_key : string -> stringCompute the Sec-WebSocket-Accept value
module Rng : sig ... endFrame parsing/serialization
val xor_mask : string -> string -> stringApply XOR mask to data
val write_frame_to_buf : is_client:bool -> Buffer.t -> frame -> unitSerialize a frame to bytes. Client frames must be masked, server frames must not be masked.
val read_exactly : [> Eio.Flow.source_ty ] Eio.Flow.source -> int -> stringRead exactly n bytes from flow
val read_frame : t -> (frame, error) resultConnection API
val is_open : t -> boolCheck if connection is open
val send : t -> frame -> (unit, error) resultSend a frame
val send_text : t -> string -> (unit, error) resultSend a text message
val send_binary : t -> string -> (unit, error) resultSend a binary message
val send_ping : t -> ?content:string -> unit -> (unit, error) resultSend a ping
val send_pong : t -> ?content:string -> unit -> (unit, error) resultSend a pong
val recv : t -> (frame, error) resultval recv_message : t -> (Opcode.t * string, error) resultReceive a complete message (handles fragmentation)
val close : ?code:int -> t -> unitClose the connection
Handshake helpers
val has_connection_upgrade_token : string -> boolCheck if request headers indicate a WebSocket upgrade
val is_upgrade_request : Http_core.Headers.t -> boolval get_websocket_key : Http_core.Headers.t -> string optionGet the Sec-WebSocket-Key from request headers
val supported_websocket_version : stringval get_websocket_version : Http_core.Headers.t -> string optionval validate_websocket_version : Http_core.Headers.t -> (unit, string) resultval get_origin : Http_core.Headers.t -> string optionGet the Origin header from request headers
type origin_policy =
| Allow_all
| Allow_list of string list
| Allow_same_originOrigin policy for WebSocket connections.
- ``Allow_all` accepts connections from any origin (NOT RECOMMENDED for production)
- ``Allow_list origins` only accepts connections from the specified origins
- ``Allow_same_origin` only accepts connections where Origin matches the Host header
val validate_origin :
policy:origin_policy ->
Http_core.Headers.t ->
(unit, string) resultValidate Origin header against policy.
parameter policy The origin validation policy parameter headers The request headers (must contain Origin, may contain Host) returns Ok () if origin is allowed, Error reason if rejected
val generate_key : unit -> stringGenerate random base64-encoded key for client handshake
Client API
val connect :
sw:Eio.Switch.t ->
net:[> [> `Generic ] Eio.Net.ty ] Eio.Net.t ->
?tls_config:Tls_config.Client.t ->
?protocols:string list ->
Uri.t ->
(t, error) resultConnect to a WebSocket server
Server API
val accept :
?max_payload_size:int ->
flow:[> Eio.Flow.two_way_ty ] Eio.Flow.sink ->
key:string ->
unit ->
(t, error) result