WebSocketClient
+ Inheritance diagram for WebSocketClient:

Description

Wraps a WebSocket peer.

See also
https://tools.ietf.org/html/rfc6455

Public Properties

const STATE_CLOSED = 2
 The peer has disconnected. More...
 
const STATE_HANDSHAKE = 0
 The peer has connected but hasn't negotiated a session yet. More...
 
const STATE_OK = 1
 The session is active and the client can perform frame I/O with the peer. More...
 
- Public Properties inherited from SocketInterface
const CH_EXCEPT = 2
 Out-of-band channel. More...
 
const CH_READ = 0
 Read channel. More...
 
const CH_WRITE = 1
 Write channel. More...
 

Protected Properties

FrameHandler $frameHandler
 
Handshake $handshake
 
WebSocketServer $server
 
int $state = self::STATE_HANDSHAKE
 
- Protected Properties inherited from AbstractSocket
resource $resource
 The underlying PHP resource. More...
 

Public Methods

 __construct ($resource, WebSocketServer $server)
 
$this close (int $code=null, string $reason='')
 Closes, optionally with a code and reason sent to the peer. More...
 
FrameHandler getFrameHandler ()
 
WebSocketServer getServer ()
 
int getState ()
 
bool isNegotiating ()
 
bool isOk ()
 
void onBinary (string $binary)
 Called when a complete BINARY payload is received from the peer. More...
 
void onClose (int $code, string $reason)
 Called when a CLOSE frame is received from the peer. More...
 
void onOutOfBand ()
 WebSockets do not use the out-of-band channel. More...
 
void onPing (string $message)
 Called when a PING is received from the peer. More...
 
void onPong (string $message)
 Called when a PONG is received from the peer. More...
 
void onReadable ()
 Delegates the read-channel to handlers. More...
 
void onText (string $text)
 Called when a complete TEXT payload is received from the peer. More...
 
void writeBinary (string $binary)
 Forwards to the FrameHandler. More...
 
void writeJSON ($data)
 JSON-encodes and sends as TEXT. More...
 
void writeText (string $text)
 Forwards to the FrameHandler. More...
 
- Public Methods inherited from StreamClient
string read (int $length)
 Reads the specified length from the peer (forced blocking). More...
 
string recv (int $maxLength, int $flags=0)
 Receives up to a specified length from the peer. More...
 
string recvAll (int $flags=0)
 All available data in the system buffer without blocking. More...
 
- Public Methods inherited from AbstractClient
string __toString ()
 The peer's name as <address>:<port>, or <pid>:0 for Unix sockets, or ?<id> if a name can't be derived (e.g. More...
 
$this connect (string $address, int $port=0)
 Connects the socket to a peer. More...
 
array getPeerName ()
 The peer's address and port, or Unix PID and port 0. More...
 
int send (string $data, int $flags=0)
 Sends data to the remote peer. More...
 
$this write (string $data)
 Sends all data (forced blocking). More...
 
- Public Methods inherited from AbstractSocket
 __construct ($resource)
 Validates and sets the underlying socket resource. More...
 
 __destruct ()
 Closes the socket if it's open. More...
 
$this await (int $channel)
 Blocks until the socket becomes available on a given channel. More...
 
$this awaitOutOfBand ()
 
$this awaitReadable ()
 
$this awaitWritable ()
 
$this close ()
 Closes the underlying resource if it's open. More...
 
int getDomain ()
 The AF_* address family constant. More...
 
int getId ()
 
mixed getOption (int $option)
 Retrieves an option value. More...
 
resource getResource ()
 
array getSockName ()
 The local address and port, or Unix file path and port 0. More...
 
bool isOpen ()
 
bool isOutOfBand ()
 Polls for whether the socket can perform a non-blocking out-of-band read. More...
 
bool isReadable ()
 Polls for whether the socket can perform a non-blocking read. More...
 
bool isReady (int $channel, ?float $timeout=0)
 Selects for channel availability. More...
 
bool isWritable ()
 Polls for whether the socket can perform a non-blocking write. More...
 
$this setBlocking (bool $blocking)
 Enables or disables blocking. More...
 
$this setOption (int $option, $value)
 Sets an option on the underlying resource. More...
 
$this setTimeout (float $timeout)
 Sets the I/O timeout length in seconds. More...
 
$this shutdown (int $channel)
 Shuts down I/O for a single channel. More...
 

Protected Methods

void onStateOk ()
 Called when the initial connection handshake succeeds and frame I/O can occur. More...
 

Additional Inherited Members

- Public Static Methods inherited from StreamClient
static int getType ()
 SOCK_STREAM More...
 
static self[] newUnixPair (... $extra)
 Creates a pair of interconnected Unix instances that can be used for IPC. More...
 
- Public Static Methods inherited from AbstractSocket
static self create (int $domain=AF_INET,... $extra)
 Creates an instance of the called class. More...
 

Properties

◆ STATE_CLOSED

const STATE_CLOSED = 2

The peer has disconnected.

◆ STATE_HANDSHAKE

const STATE_HANDSHAKE = 0

The peer has connected but hasn't negotiated a session yet.

◆ STATE_OK

const STATE_OK = 1

The session is active and the client can perform frame I/O with the peer.

Constructor

◆ __construct()

__construct (   $resource,
WebSocketServer  $server 
)
Parameters
resource$resource
WebSocketServer$server

Methods

◆ close()

$this close ( int  $code = null,
string  $reason = '' 
)

Closes, optionally with a code and reason sent to the peer.

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

The application MUST NOT send any more data frames after sending a Close frame.

After both sending and receiving a Close message, an endpoint considers the WebSocket connection closed and MUST close the underlying TCP connection.

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

Status codes in the range 0-999 are not used.

Parameters
int | null$codeSent to the peer if >= 1000
string$reasonSent to the peer, if code is >= 1000
Returns
$this

◆ getFrameHandler()

FrameHandler getFrameHandler ( )
Returns
FrameHandler

◆ getServer()

WebSocketServer getServer ( )

◆ getState()

int getState ( )
Returns
int

◆ isOk()

bool isOk ( )
final
Returns
bool

◆ onBinary()

void onBinary ( string  $binary)

Called when a complete BINARY payload is received from the peer.

Throws by default.

Parameters
string$binary
Exceptions
WebSocketError

◆ onClose()

void onClose ( int  $code,
string  $reason 
)

Called when a CLOSE frame is received from the peer.

Parameters
int$code
string$reason

◆ onOutOfBand()

void onOutOfBand ( )
final

WebSockets do not use the out-of-band channel.

The RFC says the connection must be dropped if any unsupported activity occurs.

Closes the connection with a protocol-error frame.

Implements ReactiveInterface.

◆ onPing()

void onPing ( string  $message)

Called when a PING is received from the peer.

Automatically PONGs back the payload back by default.

Parameters
string$message

◆ onPong()

void onPong ( string  $message)

Called when a PONG is received from the peer.

Does nothing by default.

Parameters
string$message

◆ onReadable()

void onReadable ( )

Delegates the read-channel to handlers.

Exceptions
WebSocketError
Throwable

Implements ReactiveInterface.

◆ onStateOk()

void onStateOk ( )
protected

Called when the initial connection handshake succeeds and frame I/O can occur.

Does nothing by default.

If you have negotiated an extension during Handshake, claim the RSV bits here via FrameReader::setRsv()

◆ onText()

void onText ( string  $text)

Called when a complete TEXT payload is received from the peer.

Throws by default.

Parameters
string$text
Exceptions
WebSocketError

◆ writeBinary()

void writeBinary ( string  $binary)

Forwards to the FrameHandler.

Parameters
string$binary

◆ writeJSON()

void writeJSON (   $data)

JSON-encodes and sends as TEXT.

Parameters
mixed$data
Exceptions
JsonException

◆ writeText()

void writeText ( string  $text)

Forwards to the FrameHandler.

Parameters
string$text