Skip to content

cWebSocketFrame Class Reference

📋 Class Overview

cWebSocketFrame is a WebSocket frame parsing and building class, used to handle WebSocket protocol frame formats.

Design Features

  • Read-only parsing - ParseHeader() does not modify input data
  • Separated unmasking - After parsing header, extract and unmask payload separately
  • Build support - Can build various types of WebSocket frames
  • Fragmentation support - Correctly handles fragmented frames (CONTINUATION)

📊 Property Reference

FIN - Final Frame Flag

Type: Boolean
Read/Write: Read-only (after parsing)

Description: Indicates whether this is the last frame of the message.

vb
If oFrame.FIN Then
    Debug.Print "This is the last frame"
Else
    Debug.Print "More frames to come"
End If

RSV1, RSV2, RSV3 - Reserved Bits

Type: Boolean
Read/Write: Read-only (after parsing)

Description: Reserved bits in the WebSocket protocol. Usually False, used for extensions.

vb
If oFrame.RSV1 Or oFrame.RSV2 Or oFrame.RSV3 Then
    Debug.Print "Extension used"
End If

OpCode - Operation Code

Type: WsOpCode (Enum)
Read/Write: Read-only (after parsing)

Values:

OpCodeConstantDescription
0WS_OPCODE_CONTINUATIONContinuation frames for fragmented messages
1WS_OPCODE_TEXTText data frame
2WS_OPCODE_BINARYBinary data frame
8WS_OPCODE_CLOSEConnection close frame
9WS_OPCODE_PINGPing frame
10WS_OPCODE_PONGPong frame
vb
Select Case oFrame.OpCode
    Case WS_OPCODE_TEXT
        Debug.Print "Text frame"
    Case WS_OPCODE_BINARY
        Debug.Print "Binary frame"
    Case WS_OPCODE_CLOSE
        Debug.Print "Close frame"
    Case WS_OPCODE_PING
        Debug.Print "Ping frame"
    Case WS_OPCODE_PONG
        Debug.Print "Pong frame"
End Select

HasMask - Has Masking

Type: Boolean
Read/Write: Read-only (after parsing)

Description: Indicates whether the payload is masked. Frames sent by client must be masked, frames sent by server should not be masked.

vb
If oFrame.HasMask Then
    Debug.Print "Frame is masked"
Else
    Debug.Print "Frame is not masked"
End If

PayloadLength - Payload Length

Type: Long
Read/Write: Read-only (after parsing)

Description: The length of the frame payload data in bytes.

vb
Debug.Print "Payload length: " & oFrame.PayloadLength & " bytes"

HeaderLength - Header Length

Type: Long
Read/Write: Read-only (after parsing)

Description: The length of the WebSocket frame header (including extended length and masking key).

vb
Debug.Print "Header length: " & oFrame.HeaderLength & " bytes"

TotalFrameLength - Total Frame Length

Type: Long
Read/Write: Read-only (after parsing)

Description: The complete frame length (header + payload).

vb
Debug.Print "Total frame length: " & oFrame.TotalFrameLength & " bytes"

IsValid - Is Valid

Type: Boolean
Read/Write: Read-only (after parsing)

Description: Whether the frame header was successfully parsed and is valid.

vb
If oFrame.IsValid Then
    Debug.Print "Frame is valid"
Else
    Debug.Print "Frame is invalid: " & oFrame.ErrorMessage
End If

ErrorMessage - Error Message

Type: String
Read/Write: Read-only (after parsing)

Description: Contains error description when frame is invalid.

vb
If Not oFrame.IsValid Then
    Debug.Print "Error: " & oFrame.ErrorMessage
End If

IsControlFrame - Is Control Frame

Type: Boolean
Read/Write: Read-only

Description: Determines whether the current frame is a control frame (CLOSE, PING, PONG).

vb
If oFrame.IsControlFrame Then
    Debug.Print "This is a control frame"
    ' Control frames cannot be fragmented
    If Not oFrame.FIN Then
        Debug.Print "Warning: Control frames should not be fragmented"
    End If
End If

IsDataFrame - Is Data Frame

Type: Boolean
Read/Write: Read-only

Description: Determines whether the current frame is a data frame (TEXT, BINARY, CONTINUATION).

vb
If oFrame.IsDataFrame Then
    Debug.Print "This is a data frame"
    ' Can be fragmented
End If

🚀 Method Reference

ParseHeader - Parse Frame Header

Syntax:

vb
Public Function ParseHeader(ByRef Buffer As cByteBuffer) As Boolean

Parameters:

ParameterTypeDescription
BuffercByteBufferByte buffer

Return Value: Boolean - Returns True if parsing successful, otherwise False

Description:

  • Read-only operation - Does not modify or consume buffer data
  • Requires at least 2 bytes of data to parse
  • After parsing, frame header information can be accessed through properties

Example:

vb
Dim oFrame As New cWebSocketFrame
Dim oBuffer As cByteBuffer

Set oBuffer = New cByteBuffer
oBuffer.Append baReceivedData

' Parse header
If oFrame.ParseHeader(oBuffer) Then
    Debug.Print "Frame type: " & oFrame.OpCode
    Debug.Print "Payload length: " & oFrame.PayloadLength
    
    ' Check if complete
    If oFrame.IsCompleteFrame(oBuffer) Then
        ' Extract payload
        Dim baPayload() As Byte
        baPayload = oFrame.ExtractPayload(oBuffer)
    End If
Else
    Debug.Print "Parsing failed: " & oFrame.ErrorMessage
End If

IsCompleteFrame - Check if Frame is Complete

Syntax:

vb
Public Function IsCompleteFrame(ByRef Buffer As cByteBuffer) As Boolean

Parameters:

ParameterTypeDescription
BuffercByteBufferByte buffer

Return Value: Boolean - Returns True if frame is complete, otherwise False

Description:

  • Must call ParseHeader() successfully first
  • Checks if buffer contains a complete frame

Example:

vb
If oFrame.ParseHeader(oBuffer) Then
    ' Check completeness
    If oFrame.IsCompleteFrame(oBuffer) Then
        ' Can extract
        baPayload = oFrame.ExtractPayload(oBuffer)
    Else
        Debug.Print "Need more data"
    End If
End If

ExtractPayload - Extract and Unmask Payload

Syntax:

vb
Public Function ExtractPayload(ByRef Buffer As cByteBuffer) As Byte()

Parameters:

ParameterTypeDescription
BuffercByteBufferByte buffer

Return Value: Byte() - Extracted payload data (unmasked)

Description:

  • Must call ParseHeader() successfully first
  • Will consume the entire frame (remove from buffer)
  • Automatically performs unmasking (if frame has masking)

Example:

vb
' Complete frame processing flow
If oFrame.ParseHeader(oBuffer) Then
    If oFrame.IsCompleteFrame(oBuffer) Then
        ' Extract payload (consume frame)
        Dim baPayload() As Byte
        baPayload = oFrame.ExtractPayload(oBuffer)
        
        ' Process payload
        ProcessPayload baPayload, oFrame.OpCode
    End If
End If

SkipFrame - Skip Frame

Syntax:

vb
Public Sub SkipFrame(ByRef Buffer As cByteBuffer)

Parameters:

ParameterTypeDescription
BuffercByteBufferByte buffer

Description: Removes the entire frame from the buffer without extracting payload. Used for processing unwanted frames.

Example:

vb
' Skip control frames
If oFrame.ParseHeader(oBuffer) Then
    If oFrame.IsControlFrame Then
        ' Skip control frame
        oFrame.SkipFrame oBuffer
    Else
        ' Process data frame
        baPayload = oFrame.ExtractPayload(oBuffer)
    End If
End If

BuildFrame - Build Frame

Syntax:

vb
Public Function BuildFrame(ByRef Payload() As Byte, _
                         ByVal OpCode As WsOpCode, _
                         Optional ByVal UseMask As Boolean = False, _
                         Optional ByVal IsFinal As Boolean = True) As Byte()

Parameters:

ParameterTypeDescription
PayloadByte()Payload data
OpCodeWsOpCodeOperation code
UseMaskBooleanWhether to mask
IsFinalBooleanWhether this is the final frame

Return Value: Byte() - Complete WebSocket frame byte array

Description:

  • Client sending must be masked (UseMask = True)
  • Server sending should not be masked (UseMask = False)
  • Automatically generates random masking key when masking

Example:

vb
' Client sending (must mask)
Dim baFrame() As Byte
Dim baData() As Byte
baData = StringToUTF8("Hello")

baFrame = oFrame.BuildFrame(baData, WS_OPCODE_TEXT, True, True)
Socket.SendData baFrame

' Server sending (no mask)
baFrame = oFrame.BuildFrame(baData, WS_OPCODE_TEXT, False, True)
Socket.SendData baFrame

BuildTextFrame - Build Text Frame

Syntax:

vb
Public Function BuildTextFrame(ByVal Text As String, _
                              Optional ByVal UseMask As Boolean = False) As Byte()

Parameters:

ParameterTypeDescription
TextStringText content
UseMaskBooleanWhether to mask

Return Value: Byte() - WebSocket text frame

Description: Text is automatically converted to UTF-8 encoding.

Example:

vb
' Client sending text
Dim baFrame() As Byte
baFrame = oFrame.BuildTextFrame("Hello WebSocket!", True)
Socket.SendData baFrame

BuildBinaryFrame - Build Binary Frame

Syntax:

vb
Public Function BuildBinaryFrame(ByRef Data() As Byte, _
                                Optional ByVal UseMask As Boolean = False) As Byte()

Parameters:

ParameterTypeDescription
Data()Byte()Binary data
UseMaskBooleanWhether to mask

Return Value: Byte() - WebSocket binary frame

Example:

vb
' Client sending binary
Dim baData() As Byte
baData = LoadFile("image.png")

Dim baFrame() As Byte
baFrame = oFrame.BuildBinaryFrame(baData, True)
Socket.SendData baFrame

BuildCloseFrame - Build Close Frame

Syntax:

vb
Public Function BuildCloseFrame(Optional ByVal StatusCode As WsCloseCode = WS_CLOSE_NORMAL, _
                               Optional ByVal Reason As String = "", _
                               Optional ByVal UseMask As Boolean = False) As Byte()

Parameters:

ParameterTypeDescription
StatusCodeWsCloseCode (Optional)Close status code
ReasonString (Optional)Close reason
UseMaskBoolean (Optional)Whether to mask

Return Value: Byte() - WebSocket close frame

Example:

vb
' Normal close
Dim baFrame() As Byte
baFrame = oFrame.BuildCloseFrame(WS_CLOSE_NORMAL, "Normal closure", True)
Socket.SendData baFrame

' Protocol error
baFrame = oFrame.BuildCloseFrame(WS_CLOSE_PROTOCOL_ERROR, "Invalid frame", True)
Socket.SendData baFrame

BuildPingFrame - Build Ping Frame

Syntax:

vb
Public Function BuildPingFrame(ByRef Payload() As Byte, _
                              Optional ByVal UseMask As Boolean = False) As Byte()

Parameters:

ParameterTypeDescription
Payload()Byte()Ping payload
UseMaskBoolean (Optional)Whether to mask

Return Value: Byte() - WebSocket Ping frame

Example:

vb
' Send empty Ping
Dim baEmpty() As Byte
Dim baFrame() As Byte
baFrame = oFrame.BuildPingFrame(baEmpty, True)
Socket.SendData baFrame

' Send Ping with data
Dim baData() As Byte
baData = StringToUTF8("ping")
baFrame = oFrame.BuildPingFrame(baData, True)
Socket.SendData baFrame

BuildPongFrame - Build Pong Frame

Syntax:

vb
Public Function BuildPongFrame(ByRef Payload() As Byte, _
                              Optional ByVal UseMask As Boolean = False) As Byte()

Parameters:

ParameterTypeDescription
Payload()Byte()Pong payload
UseMaskBoolean (Optional)Whether to mask

Return Value: Byte() - WebSocket Pong frame

Example:

vb
' Reply Pong (using Ping's payload)
Dim baFrame() As Byte
baFrame = oFrame.BuildPongFrame(baPingPayload, True)
Socket.SendData baFrame

📝 Usage Examples

Basic Frame Parsing Flow

vb
Private Sub ProcessWebSocketFrame(oBuffer As cByteBuffer)
    Dim oFrame As New cWebSocketFrame
    
    Do While oBuffer.Size >= 2
        ' 1. Parse header (read-only)
        If Not oFrame.ParseHeader(oBuffer) Then
            Debug.Print "Need more data"
            Exit Do
        End If
        
        ' 2. Check completeness
        If Not oFrame.IsCompleteFrame(oBuffer) Then
            Debug.Print "Need more data"
            Exit Do
        End If
        
        ' 3. Extract payload (consume frame)
        Dim baPayload() As Byte
        baPayload = oFrame.ExtractPayload(oBuffer)
        
        ' 4. Process frame
        Select Case oFrame.OpCode
            Case WS_OPCODE_TEXT
                Dim sText As String
                sText = UTF8ToString(baPayload)
                Debug.Print "Text: " & sText
                
            Case WS_OPCODE_BINARY
                Debug.Print "Binary: " & (UBound(baPayload) + 1) & " bytes"
                
            Case WS_OPCODE_CLOSE
                Debug.Print "Close frame"
                ProcessCloseFrame baPayload
                
            Case WS_OPCODE_PING
                Debug.Print "Ping frame"
                ' Auto reply Pong
                Dim baPong() As Byte
                baPong = oFrame.BuildPongFrame(baPayload, False)
                SendData baPong
                
            Case WS_OPCODE_PONG
                Debug.Print "Pong frame"
        End Select
    Loop
End Sub

Build and Send Frame

vb
' Client sending text
Private Sub SendTextMessage(ByVal sText As String)
    Dim oFrame As New cWebSocketFrame
    Dim baFrame() As Byte
    
    ' Build text frame (must mask)
    baFrame = oFrame.BuildTextFrame(sText, True)
    
    ' Send
    m_Socket.SendData baFrame
End Sub

' Client sending binary
Private Sub SendBinaryMessage(ByVal baData() As Byte)
    Dim oFrame As New cWebSocketFrame
    Dim baFrame() As Byte
    
    ' Build binary frame (must mask)
    baFrame = oFrame.BuildBinaryFrame(baData, True)
    
    ' Send
    m_Socket.SendData baFrame
End Sub

' Send close frame
Private Sub SendCloseFrame()
    Dim oFrame As New cWebSocketFrame
    Dim baFrame() As Byte
    
    ' Build close frame (must mask)
    baFrame = oFrame.BuildCloseFrame(WS_CLOSE_NORMAL, "Normal closure", True)
    
    ' Send
    m_Socket.SendData baFrame
End Sub

Last Updated: 2026-01-10

VB6 and LOGO copyright of Microsoft Corporation