# cp.websocket.frame

Implementation of RFC-6455, Section 5

Reads and writes data to and from websocket frame wire protocol data.


# API Overview

Functions - API calls offered directly by the extension

  • bytesRequired
  • fromBuffer
  • fromBytes
  • fromHex
  • isValid

Constructors - API calls which return an object, typically one that offers API methods

  • new

Methods - API calls which can only be made on an object returned by a constructor

  • isControlFrame
  • isNonControlFrame
  • toBytes

# API Documentation

# Functions

# bytesRequired

Signature cp.websocket.frame.bytesRequired(data) -> number | nil
Type Function
Description Checks bytes in the data string or buffer. If it contains a valid frame header (everything up to but not including the masking key/payload) it will return the total required bytes for a valid frame, otherwise it will return nil.
Parameters
  • data: the string or buffer to check.
Returns
  • The number of bytes required based on the frame header, or nil if not enough information is available.
Notes
  • The data will be unmodified after returning from this function.
Examples None
Source src/extensions/cp/websocket/frame.lua line 166

# fromBuffer

Signature cp.websocket.frame.fromBuffer(buff) -> result<{frame:frame, bytes:number}>
Type Function
Description Reads a Websocket Frame from the provided cp.buffer of binary data.
Parameters
  • buff - The cp.buffer of bytes to read from.
Returns
  • The a cp.result with either success and the frame of binary payload data plus the number of bytes read from the data,
  • or failure with a message if there was an error.
Notes
  • If a success, the value will be a table containing the following:
  • frame - The cp.websocket.frame value
  • bytes - The number of bytes which were read from the buffer.
  • If a success, the passed-in buffer will have had the bytes required for the frame removed.
  • If a failure, the passed-in buffer will not be modified.
Examples None
Source src/extensions/cp/websocket/frame.lua line 234

# fromBytes

Signature cp.websocket.frame.fromBytes(buff) -> result<{frame:frame, bytes:number}>
Type Function
Description Reads a Websocket Frame from the provided cp.buffer of binary data.
Parameters
  • buff - The cp.buffer of bytes to read from.
Returns
  • The a cp.result with either success and the frame of binary payload data plus the number of bytes read from the data,
  • or failure with a message if there was an error.
Notes
  • If a success, the value will be a table containing the following:
  • frame - The cp.websocket.frame value
  • bytes - The number of bytes which were read from the buffer.
Examples None
Source src/extensions/cp/websocket/frame.lua line 215

# fromHex

Signature cp.websocket.frame.fromHex(value, spacer) -> frame, number | nil
Type Function
Description Convenience function for converting "XX XX" strings to a binary string, then parsing it into a frame.
Parameters
  • value - The hex value as a string
  • spacer - The spacer used, for example " " (a space)
Returns
  • The frame of binary payload data plus the next index number to read from the data string, or nil if the data was invalid.
Notes None
Examples None
Source src/extensions/cp/websocket/frame.lua line 297

# isValid

Signature cp.websocket.frame.isValid(data) -> number
Type Function
Description Checks bytes in the data string or buffer contains a valid frame.
Parameters
  • data: the string or buffer to check.
Returns
  • true if the data contains both a valid frame header and sufficient bytes for the whole frame.
Notes None
Examples None
Source src/extensions/cp/websocket/frame.lua line 192

# Constructors

# new

Signature cp.websocket.frame.new(final, opcode, mask, payloadData) -> cp.websocket.frame
Type Constructor
Description Creates a new frame instance.
Parameters
  • final - If true, this is the final frame for a block of data. May be the first frame.
  • opcode - The cp.websocket.frame.opcode for the frame.
  • mask - If true, the data will be masked. Mandatory for client-originating frames.
  • payloadData - The string of application data to send.
Returns
  • The new frame instance.
Notes None
Examples None
Source src/extensions/cp/websocket/frame.lua line 311

# Methods

# isControlFrame

Signature cp.websocket.frame:isControlFrame() -> boolean
Type Method
Description Checks if the frame has a control frame opcode.
Parameters
  • None
Returns
  • true if this is a control frame.
Notes None
Examples None
Source src/extensions/cp/websocket/frame.lua line 353

# isNonControlFrame

Signature cp.websocket.frame:isNonControlFrame() -> boolean
Type Method
Description Checks if the frame has a non-control frame opcode.
Parameters
  • None
Returns
  • true if this is a non-control frame.
Notes None
Examples None
Source src/extensions/cp/websocket/frame.lua line 340

# toBytes

Signature cp.websocket.frame:toBytes() -> string
Type Method
Description Converts the frame to its byte string form.
Parameters
  • None
Returns
  • The byte string containing the frame in binary format.
Notes None
Examples None
Source src/extensions/cp/websocket/frame.lua line 373