Client

WebSocket client implementation.

package

Default

Methods

Creates new WebSocket client.

__construct(string $url, array $options = array()) 

Arguments

$url

string

Endpoint URL with schema

$options

array

Websocket client options

$options = [
  'timeout'       => (int) Number of seconds until the connect() system call should timeout
  'fragment_size' => (int) Split message to frames when exceeded fragment size limit
  'logger'        => (Psr\Log\LoggerInterface) Logger
]

Closes connection.

close(integer $status = 1000, string $reason = null, integer $timeout = self::DEFAULT_TIMEOUT_SEC) : array

Sends "close" frame.

Arguments

$status

integer

Close status code

$reason

string

Any message that will be send in close frame

$timeout

integer

Wait for response timeout

Response

array

read code and socket frame payload

$return = [
  '0' => (RtmClient\WebSocket\ReturnCode) Read result
  '1' => (string) Close frame payload
]

Establishes connection to the endpoint and upgrades connection to WebSocket.

connect() : void
Throws
\RtmClient\WebSocket\Exceptions\ConnectionException

if failed to establish connection to the endpoint

\RtmClient\WebSocket\Exceptions\ConnectionException

if server sent invalid upgrade response

\RtmClient\WebSocket\Exceptions\ConnectionException

if unable to decode Sec-WebSocket-Accept key

\RtmClient\WebSocket\Exceptions\ConnectionException

if Sec-WebSocket-Accept key was wrong

Generates pseudo bytes hex hash.

generateSecKey(integer $length = 16) : string
static

Arguments

$length

integer

Length of generated hash

Response

string

Hash

Returns current connection state.

isConnected() : boolean

Response

boolean

true if connected, false otherwise

Checks if the connection is persistent and was reused.

isReusedPersistentConnection() : boolean

Response

boolean

true if the connection is persistent and was reused. false otherwise

Splits url to parts.

parseUrl(string $url) : array
static

Arguments

$url

string

URL with scheme

Response

array

URL parts

$return = [
  'scheme'        => (string) e.g. http, wss
  'socket_scheme' => (string) ssl or tcp
  'host'          => (string)
  'path'          => (string)
  'query'         => (string)
  'port'          => (string)
]

Reads socket frame and gets payload.

read(\RtmClient\WebSocket\Client::SYNC_READ|\RtmClient\WebSocket\Client::ASYNC_READ $mode = self::SYNC_READ, integer $timeout_sec, integer $timeout_microsec) : array
Throws
\RtmClient\WebSocket\Exceptions\TimeoutException

if timeout limit is exceeded

\RtmClient\WebSocket\Exceptions\ConnectionException

if not connected

\RtmClient\WebSocket\Exceptions\ConnectionException

if socket frame is broken

\RtmClient\WebSocket\Exceptions\ConnectionException

if read from closed connection

\RtmClient\WebSocket\Exceptions\ConnectionException

if unable to read from stream

Arguments

$timeout_sec

integer

The seconds part of the timeout to be set if read in Sync mode

$timeout_microsec

integer

The microseconds part of the timeout to be set if read in Sync mode

Response

array

read code and socket frame payload

$return = [
  '0' => (RtmClient\WebSocket\ReturnCode) Read result
  '1' => (string) Frame payload
]

Reads socket frame.

readFrame(\RtmClient\WebSocket\Client::SYNC_READ|\RtmClient\WebSocket\Client::ASYNC_READ $mode, integer $timeout_sec, integer $timeout_microsec) : array

Arguments

$timeout_sec

integer

The seconds part of the timeout to be set if read in Sync mode

$timeout_microsec

integer

The microseconds part of the timeout to be set if read in Sync mode

Response

array

Read result

$return = [
  '0' => (RtmClient\WebSocket\ReturnCode) Read result
  '1' => (string) Frame payload
]

Sends payload to server.

send(string $payload, boolean $masked = true, \RtmClient\WebSocket\OpCode $opcode = \RtmClient\WebSocket\OpCode::TEXT) : true
Throws
\RtmClient\WebSocket\Exceptions\ConnectionException

if client is not connected

\RtmClient\WebSocket\Exceptions\ConnectionException

if data was not sent or if sent bytes are not equal to payload

Arguments

$payload

string

Payload data

$masked

boolean

Use masking for message flag

$opcode

\RtmClient\WebSocket\OpCode

Defines the interpretation of the $payload

Response

true

if payload was sent

Sends fragment.

sendFragment(boolean $final, string $payload, \RtmClient\WebSocket\OpCode $opcode, boolean $masked) : integer
Throws
\RtmClient\WebSocket\Exceptions\ConnectionException

if bytes sent did not match actual payload size

Arguments

$final

boolean

Mark fragment as Final

$payload

string

Payload data

$opcode

\RtmClient\WebSocket\OpCode

Fragmant OpCode

$masked

boolean

Use mask for fragment

Response

integer

bytes sent

Reads from socket.

socketRead(integer $length, \RtmClient\WebSocket\Client::SYNC_READ|\RtmClient\WebSocket\Client::ASYNC_READ $mode = self::SYNC_READ) : string
Throws
\RtmClient\WebSocket\Exceptions\TimeoutException

if timeout limit is exceeded

\RtmClient\WebSocket\Exceptions\ConnectionException

if not connected

\RtmClient\WebSocket\Exceptions\ConnectionException

if socket frame is broken

\RtmClient\WebSocket\Exceptions\ConnectionException

if read from closed connection

\RtmClient\WebSocket\Exceptions\ConnectionException

if unable to read from stream

Arguments

$length

integer

Amount of bytes to be read from socket

Response

string

Read data

Writes data to socket.

socketWrite(string $data) : integer
Throws
\RtmClient\WebSocket\Exceptions\ConnectionException

if bytes sent did not match actual data size

Arguments

$data

string

Data to be send

Response

integer

bytes sent

streamIsReadyToRead

streamIsReadyToRead() 

Converts string to binary representation.

strToBin(string $str) : string
static

Arguments

$str

string

String to be converted to binary

Response

string

Binary representation

Constants

Number of seconds until the connect() system call should timeout

DEFAULT_TIMEOUT_SEC

Split message to frames when exceeded fragment size limit

DEFAULT_FRAGMENT_SIZE

WEBSOCKET_MAGIC_KEY

WEBSOCKET_MAGIC_KEY

Sync mode code

SYNC_READ

Async mode code

ASYNC_READ

Properties

stream_socket_client instance

socket : resource
var

Type(s)

resource

Current client state

is_connected : boolean
var

Type(s)

boolean

Flag used for peristent connection status

is_reused_p_connection : boolean
var

Type(s)

boolean

Socket state after call close()

is_closing : boolean
var

Type(s)

boolean

Frame processing flag.

frame_processing_in_progress : boolean

Uses to drop a connection in case if script died, but only a part of a frame was read or sent.

var

Type(s)

boolean