FrameHandler

Description

Interprets parsed frames from the peer, and packs and writes frames.

TODO: Multiplex by RSV.

TODO: Stream writing.

Protected Properties

string $buffer = ''
 The message buffer for data frames. More...
 
WebSocketClient $client
 
int null $continue
 Resume opCode for the CONTINUATION handler. More...
 
int $fragmentSize = 128 * 1024
 Max outgoing fragment size. More...
 
int $maxLength = 10 * 1024 * 1024
 Maximum inbound message length (complete payload). More...
 
FrameReader $reader
 
bool $stream = false
 Whether binary I/O should bypass buffers. More...
 

Public Methods

 __construct (WebSocketClient $client)
 
int getFragmentSize ()
 
int getMaxLength ()
 
bool isStream ()
 
void onFrame (Frame $frame)
 Called by WebSocketClient when a complete frame has been received. More...
 
void onReadable ()
 Uses FrameReader to read frames and passes them off to onFrame(). More...
 
$this setFragmentSize (int $bytes)
 
$this setMaxLength (int $bytes)
 
$this setStream (bool $stream)
 
void write (int $opCode, string $payload)
 Sends a complete message to the peer, fragmenting if needed. More...
 
void writeBinary (string $payload)
 
void writeClose (int $code=Frame::CLOSE_NORMAL, string $reason='')
 
void writePing (string $payload='')
 
void writePong (string $payload='')
 
void writeText (string $payload)
 

Protected Methods

void onBinary (Frame $frame)
 Progressively receives BINARY data into the buffer until the payload is complete. More...
 
void onClose (Frame $frame)
 When a CLOSE frame is received. More...
 
void onContinuation (Frame $frame)
 When a CONTINUATION frame (data fragment) is received. More...
 
void onControl (Frame $frame)
 When a control frame is received. More...
 
void onData (Frame $frame)
 When an initial data frame (not CONTINUATION) is received. More...
 
void onData_CheckLength (Frame $frame)
 Validates the message length, but only when the buffer is in use. More...
 
void onPing (Frame $frame)
 When a PING is received. More...
 
void onPong (Frame $frame)
 When a PONG is received. More...
 
void onText (Frame $frame)
 Progressively receives TEXT data until the payload is complete. More...
 
void writeFrame (bool $final, int $opCode, string $payload)
 Writes a single frame. More...
 

Properties

◆ $buffer

string $buffer = ''
protected

The message buffer for data frames.

◆ $continue

int null $continue
protected

Resume opCode for the CONTINUATION handler.

◆ $fragmentSize

int $fragmentSize = 128 * 1024
protected

Max outgoing fragment size.

Each browser has its own standard, so this is generalized.

Defaults to 128 KiB.

◆ $maxLength

int $maxLength = 10 * 1024 * 1024
protected

Maximum inbound message length (complete payload).

Defaults to 10 MiB.

◆ $stream

bool $stream = false
protected

Whether binary I/O should bypass buffers.

Constructor

◆ __construct()

__construct ( WebSocketClient  $client)
Parameters
WebSocketClient$client

Methods

◆ getFragmentSize()

int getFragmentSize ( )
Returns
int

◆ getMaxLength()

int getMaxLength ( )
Returns
int

◆ isStream()

bool isStream ( )
Returns
bool

◆ onBinary()

void onBinary ( Frame  $frame)
protected

Progressively receives BINARY data into the buffer until the payload is complete.

Passes the complete payload up to WebSocketClient::onBinary()

When $stream is true, this bypasses the buffer.

Parameters
Frame$frame
Exceptions
WebSocketError

◆ onClose()

void onClose ( Frame  $frame)
protected

When a CLOSE frame is received.

Calls WebSocketClient::onClose()

https://tools.ietf.org/html/rfc6455#section-5.5.1

If an endpoint receives a Close frame and did not previously send a Close frame, the endpoint MUST send a Close frame in response. (When sending a Close frame in response, the endpoint typically echos the status code it received.)

Parameters
Frame$frame

◆ onContinuation()

void onContinuation ( Frame  $frame)
protected

When a CONTINUATION frame (data fragment) is received.

Parameters
Frame$frame
Exceptions
WebSocketError

◆ onControl()

void onControl ( Frame  $frame)
protected

When a control frame is received.

https://tools.ietf.org/html/rfc6455#section-5.4

Control frames (see Section 5.5) MAY be injected in the middle of a fragmented message.

Parameters
Frame$frame

◆ onData()

void onData ( Frame  $frame)
protected

When an initial data frame (not CONTINUATION) is received.

Parameters
Frame$frame

◆ onData_CheckLength()

void onData_CheckLength ( Frame  $frame)
protected

Validates the message length, but only when the buffer is in use.

Parameters
Frame$frame
Exceptions
WebSocketError

◆ onFrame()

void onFrame ( Frame  $frame)

Called by WebSocketClient when a complete frame has been received.

Delegates to the other handler methods using the program logic outlined in the RFC.

Eventually calls back to the WebSocketClient when payloads are complete.

Parameters
Frame$frame

◆ onPing()

void onPing ( Frame  $frame)
protected

When a PING is received.

Calls WebSocketClient::onPing()

Parameters
Frame$frame

◆ onPong()

void onPong ( Frame  $frame)
protected

When a PONG is received.

Calls WebSocketClient::onPong()

Parameters
Frame$frame

◆ onReadable()

void onReadable ( )

Uses FrameReader to read frames and passes them off to onFrame().

◆ onText()

void onText ( Frame  $frame)
protected

Progressively receives TEXT data until the payload is complete.

Validates the complete payload as UTF-8 and passes it up to WebSocketClient::onText()

Parameters
Frame$frame
Exceptions
WebSocketError

◆ setFragmentSize()

$this setFragmentSize ( int  $bytes)
Parameters
int$bytes
Returns
$this

◆ setMaxLength()

$this setMaxLength ( int  $bytes)
Parameters
int$bytes
Returns
$this

◆ setStream()

$this setStream ( bool  $stream)
Parameters
bool$stream
Returns
$this

◆ write()

void write ( int  $opCode,
string  $payload 
)

Sends a complete message to the peer, fragmenting if needed.

Parameters
int$opCode
string$payload

◆ writeBinary()

void writeBinary ( string  $payload)
Parameters
string$payload

◆ writeClose()

void writeClose ( int  $code = Frame::CLOSE_NORMAL,
string  $reason = '' 
)
Parameters
int$code
string$reason

◆ writeFrame()

void writeFrame ( bool  $final,
int  $opCode,
string  $payload 
)
protected

Writes a single frame.

Parameters
bool$final
int$opCode
string$payload

◆ writePing()

void writePing ( string  $payload = '')
Parameters
string$payload

◆ writePong()

void writePong ( string  $payload = '')
Parameters
string$payload

◆ writeText()

void writeText ( string  $payload)
Parameters
string$payload