object
http_websocket_session(Role,TextRepresentation)
Role- Peer role for masking policy. Possible values areclientandserver.TextRepresentation- Text representation to be used for text messages and close reasons. Possible values areatom(default),chars, andcodes.
Stateful WebSocket session predicates that build on top of the message layer to surface interleaved control frames, apply role-aware masking policies, and track close-handshake state. The callback-driven session loops are provided by the http_websocket_service library.
logtalk_load(http_websocket_session(loader))static, context_switching_calls
Option precedence: When the same read or write option is given multiple times, the first occurrence is used.
Public predicates
initial_state/1
Returns the initial session state term to be threaded through stateful reads.
staticinitial_state(State)initial_state(-compound) - oneis_state/1
True when the argument is a valid session state term.
staticis_state(State)is_state(@term) - zero_or_oneread_message/4
Reads the next session-level WebSocket message from a binary stream using the given input state. Fragmented text and binary messages are reassembled across calls, interleaved control frames are surfaced immediately, and close-message state transitions are tracked in the returned state.
staticread_message(Stream,State,UpdatedState,Message)read_message(+stream_or_alias,+compound,-compound,-term) - one_or_errorStream is a variable:instantiation_errorclient nor server:domain_error(http_websocket_session_role,_Role_)State is not a valid WebSocket session state:domain_error(http_websocket_session_state,State)domain_error(http_websocket_session_sequence,Frame)domain_error(http_websocket_session_masking,Frame)domain_error(http_websocket_session_extensions,Frame)domain_error(http_websocket_frame,Frame)domain_error(http_websocket_message_text,Payload)
End of file: Returns
end_of_fileonly when the input state is idle and the stream is already exhausted.Masking policy: Incoming frame masking is validated against the session role: client sessions expect unmasked inbound frames and server sessions expect masked inbound frames.
Peer close: When a close frame is read, any fragmented data message in progress is discarded. Subsequent reads from the resulting
close_received/1orclosed/2state are rejected.
read_message/5
Reads the next session-level WebSocket message from a binary input stream using the given state and automatically orchestrates the close handshake on the given binary output stream when a close message is received.
staticread_message(Input,Output,State,UpdatedState,Message)read_message(+stream_or_alias,+stream_or_alias,+compound,-compound,-term) - one_or_errorInput is a variable:instantiation_errorclient nor server:domain_error(http_websocket_session_role,_Role_)State is not a valid WebSocket session state:domain_error(http_websocket_session_state,State)domain_error(http_websocket_session_sequence,Frame)domain_error(http_websocket_session_masking,Frame)domain_error(http_websocket_session_extensions,Frame)domain_error(http_websocket_frame,Frame)domain_error(http_websocket_message_text,Payload)
Close handshake: When a close message is received and no close message has yet been sent for the tracked session state, a matching close message is written automatically on the output stream and the returned state records the completed handshake.
read_message/6
Reads the next session-level WebSocket message from a binary input stream using the given state, automatically orchestrates the close handshake on the given binary output stream, and applies optional automatic control-message policies.
staticread_message(Input,Output,State,UpdatedState,Message,Options)read_message(+stream_or_alias,+stream_or_alias,+compound,-compound,-term,+list) - one_or_errorInput is a variable:instantiation_errorclient nor server:domain_error(http_websocket_session_role,_Role_)State is not a valid WebSocket session state:domain_error(http_websocket_session_state,State)Options is a variable or a partial list:instantiation_errorOptions is neither a variable nor a list:type_error(list,Options)Option of the list Options is neither a variable nor a compound term:type_error(compound,Option)Option of the list Options is a compound term but not a valid read option:domain_error(http_websocket_session_read_option,Option)domain_error(http_websocket_session_sequence,Frame)domain_error(http_websocket_session_masking,Frame)domain_error(http_websocket_session_extensions,Frame)domain_error(http_websocket_payload_length_limit,PayloadLength)
Option
auto_pong(on): Automatically writes a pong message with the same payload when a ping message is read while still returning the ping message to the caller.Option
auto_pong(off): Disables automatic pong replies. This is the default.Option
max_payload_length(Bytes): Rejects inbound frames whose declared payload length is greater thanBytesbefore allocating payload storage. Use a non-negative integer.
write_message/2
Stateless convenience wrapper that writes one validated WebSocket message using the masking policy implied by the session role. Client sessions mask all outgoing frames; server sessions leave them unmasked.
staticwrite_message(Stream,Message)write_message(+stream_or_alias,+compound) - one_or_errorStream is a variable:instantiation_errorclient nor server:domain_error(http_websocket_session_role,_Role_)Message is not a valid normalized WebSocket message term:domain_error(http_websocket_message,Message)Message contains invalid text:domain_error(http_websocket_message_text,Text)domain_error(http_websocket_frame,Frame)write_message/3
Stateless convenience wrapper that writes one validated WebSocket message using the masking policy implied by the session role and the given write options.
staticwrite_message(Stream,Message,Options)write_message(+stream_or_alias,+compound,+list) - one_or_errorStream is a variable:instantiation_errorclient nor server:domain_error(http_websocket_session_role,_Role_)Message is not a valid normalized WebSocket message term:domain_error(http_websocket_message,Message)Options is a variable or a partial list:instantiation_errorOptions is neither a variable nor a list:type_error(list,Options)Option of the list Options is neither a variable nor a compound term:type_error(compound,Option)Option of the list Options is a compound term but not a valid write option:domain_error(http_websocket_session_write_option,Option)domain_error(http_websocket_frame,Frame)
Option
fragment_size(Size): When writingtextorbinarymessages, split the payload into frames of at mostSizebytes. Control messages always remain single final frames.
write_message/4
Writes one validated WebSocket message using the masking policy implied by the session role and updates the given session state with any close-handshake transition caused by the write.
staticwrite_message(Stream,State,UpdatedState,Message)write_message(+stream_or_alias,+compound,-compound,+compound) - one_or_errorStream is a variable:instantiation_errorclient nor server:domain_error(http_websocket_session_role,_Role_)State is not a valid WebSocket session state:domain_error(http_websocket_session_state,State)Message is not a valid normalized WebSocket message term:domain_error(http_websocket_message,Message)domain_error(http_websocket_session_state,State)
Closing state: After a peer close is recorded, only the matching close reply may be written. After a local close is recorded, further data messages are rejected.
write_message/5
Writes one validated WebSocket message using the masking policy implied by the session role and the given write options while updating the given session state with any close-handshake transition caused by the write.
staticwrite_message(Stream,State,UpdatedState,Message,Options)write_message(+stream_or_alias,+compound,-compound,+compound,+list) - one_or_errorStream is a variable:instantiation_errorclient nor server:domain_error(http_websocket_session_role,_Role_)State is not a valid WebSocket session state:domain_error(http_websocket_session_state,State)Message is not a valid normalized WebSocket message term:domain_error(http_websocket_message,Message)Options is a variable or a partial list:instantiation_errorOptions is neither a variable nor a list:type_error(list,Options)Option of the list Options is neither a variable nor a compound term:type_error(compound,Option)Option of the list Options is a compound term but not a valid write option:domain_error(http_websocket_session_write_option,Option)domain_error(http_websocket_session_state,State)
Option
fragment_size(Size): When writingtextorbinarymessages, split the payload into frames of at mostSizebytes. Control messages always remain single final frames.
Protected predicates
validate_role/0
Validates the session role parameter. Throws a domain error when the role is neither client nor server.
staticvalidate_role - one_or_errorclient nor server:domain_error(http_websocket_session_role,_Role_)read_session_message/5
Protected helper that validates the session role and state and reads the next session-level message, returning the updated pending-fragment bookkeeping term and the message. Close-state transitions are left to the caller.
staticread_session_message(Stream,State,MaxPayloadLength,Pending,Message)read_session_message(+stream_or_alias,+compound,+term,-compound,-term) - one_or_errorclient nor server:domain_error(http_websocket_session_role,_Role_)State is not a valid WebSocket session state:domain_error(http_websocket_session_state,State)domain_error(http_websocket_session_sequence,Frame)domain_error(http_websocket_session_masking,Frame)domain_error(http_websocket_session_extensions,Frame)apply_session_read/6
Protected helper that applies the close-state and automatic control-message transitions for a message read outside this object and returns the updated session state.
staticapply_session_read(Output,State,AutoPong,Pending,Message,UpdatedState)apply_session_read(+stream_or_alias,+compound,+atom,+compound,+term,-compound) - one_or_errorState is not a valid WebSocket session state:domain_error(http_websocket_session_state,State)AutoPong is not a supported automatic pong setting:domain_error(http_websocket_session_read_option,auto_pong(AutoPong))Message is not a valid normalized WebSocket message term:domain_error(http_websocket_message,Message)current_close_state/2
Protected helper that returns the close-handshake component of a session state term. Open states are reported as open.
staticcurrent_close_state(State,CloseState)current_close_state(+compound,-compound) - oneclose_handshake_completed/1
Protected helper that is true when the given session state records a completed close handshake.
staticclose_handshake_completed(State)close_handshake_completed(+compound) - zero_or_onebest_effort_protocol_error_close/3
Protected helper that writes a best-effort close message mapping the given protocol error to its close status code when the session close state still allows it. Write errors are ignored.
staticbest_effort_protocol_error_close(Output,State,Error)best_effort_protocol_error_close(+stream_or_alias,+compound,+callable) - onegenerate_masking_key/1
Protected hook that returns the four-byte masking key to use for outgoing client frames. The default implementation delegates to crypto::random_bytes/2.
staticgenerate_masking_key(Key)generate_masking_key(-list(byte)) - onePrivate predicates
(no local declarations; see entity ancestors if any)
Operators
(none)