Documentation

Client
in package
implements LoggerAwareInterface, Stringable uses ListenerTrait, SendMethodsTrait, StringableTrait

WebSocket\Client class.

Entry class for WebSocket client.

Table of Contents

Interfaces

LoggerAwareInterface
Describes a logger-aware instance.
Stringable

Properties

$connection  : Connection|null
$context  : array<string|int, mixed>
$frameSize  : int
$headers  : array<string|int, mixed>
$listeners  : array<string|int, mixed>
$logger  : LoggerInterface
$middlewares  : array<string|int, mixed>
$persistent  : bool
$running  : bool
$socketUri  : Uri
$streamFactory  : StreamFactory
$streams  : StreamCollection|null
$timeout  : int

Methods

__construct()  : mixed
__toString()  : string
Get string representation of instance.
addHeader()  : self
Add header for handshake.
addMiddleware()  : self
Add a middleware.
binary()  : Binary
Send binary message.
close()  : Close
Tell the socket to close.
connect()  : void
Connect to server and perform upgrade.
disconnect()  : void
Disconnect from server.
getFrameSize()  : int
Get frame size.
getHandshakeResponse()  : Response|null
Get Response for handshake procedure.
getMeta()  : mixed
Get meta value on connection.
getName()  : string|null
Get name of local socket, or null if not connected.
getRemoteName()  : string|null
Get name of remote socket, or null if not connected.
getTimeout()  : int
Get timeout.
isConnected()  : bool
If Client has active connection.
isReadable()  : bool
If Client is readable.
isRunning()  : bool
If client is running (accepting messages).
isWritable()  : bool
If Client is writable.
onBinary()  : self
onClose()  : self
onConnect()  : self
onDisconnect()  : self
onError()  : self
onPing()  : self
onPong()  : self
onText()  : self
onTick()  : self
ping()  : Ping
Send ping.
pong()  : Pong
Send unsolicited pong.
receive()  : Message
Receive message.
send()  : Message
Send message.
setContext()  : self
Set connection context.
setFrameSize()  : self
Set frame size.
setLogger()  : void
Set logger.
setPersistent()  : self
Set connection persistence.
setStreamFactory()  : self
Set stream factory to use.
setTimeout()  : self
Set timeout.
start()  : void
Start client listener.
stop()  : void
Stop client listener (resumable).
text()  : Text
Send text message.
generateKey()  : string
Generate a random string for WebSocket key.
parseUri()  : Uri
Ensure URI instance to use in client.
performHandshake()  : Response
Perform upgrade handshake on new connections.
stringable()  : string
dispatch()  : void

Properties

$context

private array<string|int, mixed> $context = []

$frameSize

private int $frameSize = 4096

$headers

private array<string|int, mixed> $headers = []

$middlewares

private array<string|int, mixed> $middlewares = []

$persistent

private bool $persistent = false

$running

private bool $running = false

$timeout

private int $timeout = 60

Methods

__construct()

public __construct(UriInterface|string $uri) : mixed
Parameters
$uri : UriInterface|string

A ws/wss-URI

__toString()

Get string representation of instance.

public __toString() : string
Return values
string

String representation

addHeader()

Add header for handshake.

public addHeader(string $name, string $content) : self
Parameters
$name : string

Header name

$content : string

Header content

Return values
self

addMiddleware()

Add a middleware.

public addMiddleware(MiddlewareInterface $middleware) : self
Parameters
$middleware : MiddlewareInterface
Return values
self

binary()

Send binary message.

public binary(string $message) : Binary
Parameters
$message : string

Content as binary string.

Return values
Binary

instance

close()

Tell the socket to close.

public close([int $status = 1000 ][, string $message = 'ttfn' ]) : Close
Parameters
$status : int = 1000

http://tools.ietf.org/html/rfc6455#section-7.4

$message : string = 'ttfn'

A closing message, max 125 bytes.

Return values
Close

instance

connect()

Connect to server and perform upgrade.

public connect() : void
Tags
throws
ClientException

On failed connection

disconnect()

Disconnect from server.

public disconnect() : void

getFrameSize()

Get frame size.

public getFrameSize() : int
Return values
int

Frame size in bytes

getHandshakeResponse()

Get Response for handshake procedure.

public getHandshakeResponse() : Response|null
Return values
Response|null

Handshake.

getMeta()

Get meta value on connection.

public getMeta(string $key) : mixed
Parameters
$key : string

Meta key

Return values
mixed

Meta value

getName()

Get name of local socket, or null if not connected.

public getName() : string|null
Return values
string|null

getRemoteName()

Get name of remote socket, or null if not connected.

public getRemoteName() : string|null
Return values
string|null

getTimeout()

Get timeout.

public getTimeout() : int
Return values
int

Timeout in seconds

isConnected()

If Client has active connection.

public isConnected() : bool
Return values
bool

True if active connection.

isReadable()

If Client is readable.

public isReadable() : bool
Return values
bool

isRunning()

If client is running (accepting messages).

public isRunning() : bool
Return values
bool

isWritable()

If Client is writable.

public isWritable() : bool
Return values
bool

onBinary()

public onBinary(Closure $closure) : self
Parameters
$closure : Closure
Return values
self

onClose()

public onClose(Closure $closure) : self
Parameters
$closure : Closure
Return values
self

onConnect()

public onConnect(Closure $closure) : self
Parameters
$closure : Closure
Return values
self

onDisconnect()

public onDisconnect(Closure $closure) : self
Parameters
$closure : Closure
Return values
self

onError()

public onError(Closure $closure) : self
Parameters
$closure : Closure
Return values
self

onPing()

public onPing(Closure $closure) : self
Parameters
$closure : Closure
Return values
self

onPong()

public onPong(Closure $closure) : self
Parameters
$closure : Closure
Return values
self

onText()

public onText(Closure $closure) : self
Parameters
$closure : Closure
Return values
self

onTick()

public onTick(Closure $closure) : self
Parameters
$closure : Closure
Return values
self

ping()

Send ping.

public ping([string $message = '' ]) : Ping
Parameters
$message : string = ''

Optional text as string.

Return values
Ping

instance

pong()

Send unsolicited pong.

public pong([string $message = '' ]) : Pong
Parameters
$message : string = ''

Optional text as string.

Return values
Pong

instance

receive()

Receive message.

public receive() : Message

Note that this operation will block reading.

Return values
Message

setContext()

Set connection context.

public setContext(array<string|int, mixed> $context) : self
Parameters
$context : array<string|int, mixed>

Context as array, see https://www.php.net/manual/en/context.php

Return values
self

setFrameSize()

Set frame size.

public setFrameSize(int $frameSize) : self
Parameters
$frameSize : int

Frame size in bytes

Tags
throws
InvalidArgumentException

If invalid frameSize provided

Return values
self

setLogger()

Set logger.

public setLogger(LoggerInterface $logger) : void
Parameters
$logger : LoggerInterface

Logger implementation

setPersistent()

Set connection persistence.

public setPersistent(bool $persistent) : self
Parameters
$persistent : bool

True for persistent connection.

Return values
self

.

setStreamFactory()

Set stream factory to use.

public setStreamFactory(StreamFactory $streamFactory) : self
Parameters
$streamFactory : StreamFactory
Return values
self

setTimeout()

Set timeout.

public setTimeout(int $timeout) : self
Parameters
$timeout : int

Timeout in seconds

Tags
throws
InvalidArgumentException

If invalid timeout provided

Return values
self

start()

Start client listener.

public start() : void
Tags
throws
Throwable

On low level error

stop()

Stop client listener (resumable).

public stop() : void

text()

Send text message.

public text(string $message) : Text
Parameters
$message : string

Content as string.

Return values
Text

instance

generateKey()

Generate a random string for WebSocket key.

protected generateKey() : string
Return values
string

Random string

stringable()

protected stringable(string $format, mixed ...$values) : string
Parameters
$format : string
$values : mixed
Return values
string

dispatch()

private dispatch(string $type[, array<string|int, mixed> $args = [] ]) : void
Parameters
$type : string
$args : array<string|int, mixed> = []

        
On this page

Search results