Qore WebSocketUtil Module Reference 1.6
Loading...
Searching...
No Matches
WebSocketUtil Namespace Reference

the WebSocketUtil namespace contains all the definitions in the WebSocketUtil module More...

Classes

class  AbstractWebSocketExtension
 Abstract base class for WebSocket extensions. More...
 
class  PerMessageDeflateExtension
 Implementation of the permessage-deflate extension (RFC 7692) More...
 
class  WebSocketExtensionRegistry
 Registry for WebSocket extensions. More...
 
hashdecl  WsExtensionInfo
 Extension offer/response info. More...
 
hashdecl  WsExtensionParamInfo
 Extension parameter info. More...
 
hashdecl  WsMsgInfo
 WebSocket message info. More...
 

Functions

binary ws_encode_message (data msg, int op, soft< bool > masked, bool fin, int rsv_bits)
 encodes a message for sending over a websocket socket
 
soft< int > ws_get_frame_size (binary buf)
 Returns the total size needed for a complete WebSocket frame, or NOTHING if not enough header data.
 
string ws_get_response_key (string key)
 returns a string response key from the binary key and the WebSocket GUID value
 
hash< WsMsgInfows_parse_frame (binary buf, soft< string > encoding)
 Parses a WebSocket frame from binary data.
 
hash< WsMsgInfows_read_message (Socket sock, soft< timeout > to)
 read and decode a message from a socket
 

Variables

const auto WS_FIN = ...
 the final fragment in a message
 
const auto WS_GUID = ...
 WebSocket GUID.
 
const auto WS_RSV1 = ...
 RSV1 bit - used by permessage-deflate extension.
 
const auto WS_RSV2 = ...
 RSV2 bit - reserved for extensions.
 
const auto WS_RSV3 = ...
 RSV3 bit - reserved for extensions.
 
const auto WSCC_AbnormalClosure = ...
 "Abnormal Closure" code
 
const auto WSCC_GoingAway = ...
 "Going Away" code
 
const auto WSCC_InternalServerError = ...
 "Internal Server Error" code
 
const auto WSCC_InvalidData = ...
 "Invalid Frame Payload Data" code
 
const auto WSCC_MandatoryExt = ...
 "Mandatory Ext." code
 
const auto WSCC_MessageTooBig = ...
 "Message Too Big" code
 
const auto WSCC_NormalClosure = ...
 "Normal Closure" code
 
const auto WSCC_NoStatusRcvd = ...
 "No Status Rcvd" code
 
const auto WSCC_PolicyViolation = ...
 "Policy Violation" code
 
const auto WSCC_ProtocolError = ...
 "Protocol Error" code
 
const auto WSCC_TlsHandshake = ...
 "TLS Handshake" code
 
const auto WSCC_UnsupportedData = ...
 "Unsupported Data" code
 
const auto WSCCMap = ...
 maps from close codes to text descriptions
 
const auto WSOP_Binary = ...
 binary frame opcode
 
const auto WSOP_Close = ...
 connection code opcode
 
const auto WSOP_Continuation = ...
 continuation frame opcode
 
const auto WSOP_Ping = ...
 ping opcode
 
const auto WSOP_Pong = ...
 pong opcode
 
const auto WSOP_Text = ...
 text frame opcode
 
const auto WSOPMap = ...
 maps from opcodes to text descriptions
 

Detailed Description

the WebSocketUtil namespace contains all the definitions in the WebSocketUtil module

Function Documentation

◆ ws_encode_message()

binary WebSocketUtil::ws_encode_message ( data  msg,
int  op,
soft< bool >  masked,
bool  fin,
int  rsv_bits 
)

encodes a message for sending over a websocket socket

Parameters
msgthe message to encode
opthe opcode to use; if -1 (the default), the opcode is determined from the message type
maskedif True, the message is masked with a random mask
finif True (the default), the FIN bit is set
rsv_bitsoptional RSV bits to set (bitmask of WS_RSV1, WS_RSV2, WS_RSV3); these are used by extensions like permessage-deflate
Returns
the encoded WebSocket frame as binary data
Since
WebSocketUtil 1.5 added the rsv_bits parameter

◆ ws_get_frame_size()

soft< int > WebSocketUtil::ws_get_frame_size ( binary  buf)

Returns the total size needed for a complete WebSocket frame, or NOTHING if not enough header data.

Parameters
bufthe buffer containing at least the beginning of a WebSocket frame
Returns
the total number of bytes needed for a complete frame, or NOTHING if the buffer doesn't contain enough data to determine the frame size (need at least 2 bytes for header, plus extended length bytes if applicable)
Since
WebSocketUtil 1.7

◆ ws_get_response_key()

string WebSocketUtil::ws_get_response_key ( string  key)

returns a string response key from the binary key and the WebSocket GUID value

Parameters
keythe base64-encoded key received in the Sec-WebSocket-Key header
Returns
the base64-encoded string value for the Sec-WebSocket-Accept header
Since
WebSocketUtil 1.4

◆ ws_parse_frame()

hash< WsMsgInfo > WebSocketUtil::ws_parse_frame ( binary  buf,
soft< string >  encoding 
)

Parses a WebSocket frame from binary data.

Parameters
bufthe binary data containing the WebSocket frame
encodingthe character encoding to use for text messages
Returns
a hash with the following keys:
  • op: the operation code (one of ws_op_codes)
  • msg: the message data received, if any
  • masked: True if the data was masked
  • close: for close messages, the close code
  • fin: True if this is the final frame in a message
  • rsv1: the RSV1 bit (used by extensions like permessage-deflate)
  • rsv2: the RSV2 bit (reserved for extensions)
  • rsv3: the RSV3 bit (reserved for extensions)
  • info: user-defined info; may include debug-only metadata
Exceptions
WEBSOCKET-ERRORif the buffer is too short to contain a valid frame
Note
This function is used for HTTP/2 WebSocket where frame data comes from HTTP/2 DATA frames rather than directly from the socket
Since
WebSocketUtil 1.6

◆ ws_read_message()

hash< WsMsgInfo > WebSocketUtil::ws_read_message ( Socket  sock,
soft< timeout >  to 
)

read and decode a message from a socket

Example:
hash<WsMsgInfo> h = ws_read_message(sock);
Parameters
sockthe Socket object to receive the message
toan optional read timeout
Returns
a hash with the following keys:
  • op: the operation code (one of opcodes)
  • masked a boolean flag indicating if the message was masked or not
  • msg: the message received; if a CLOSE opcode is received (see WSOP_Close) then any close message is decoded and included here in text form
  • close: the close code (one of closecodes); only included if op is WSOP_Close
  • fin: True if this is the final frame in a message
  • rsv1: the RSV1 bit (used by extensions like permessage-deflate)
  • rsv2: the RSV2 bit (reserved for extensions)
  • rsv3: the RSV3 bit (reserved for extensions)
  • info: user-defined info; may include debug-only metadata
Since
WebSocketUtil 1.5 added rsv1, rsv2, rsv3 fields

Variable Documentation

◆ WS_RSV1

const auto WebSocketUtil::WS_RSV1 = ...

RSV1 bit - used by permessage-deflate extension.

Since
WebSocketUtil 1.5

◆ WS_RSV2

const auto WebSocketUtil::WS_RSV2 = ...

RSV2 bit - reserved for extensions.

Since
WebSocketUtil 1.5

◆ WS_RSV3

const auto WebSocketUtil::WS_RSV3 = ...

RSV3 bit - reserved for extensions.

Since
WebSocketUtil 1.5