Wrench\Socket\Socket

class Socket

Socket class

Implements low level logic for connecting, serving, reading to, and writing from WebSocket connections using PHP’s streams.

Unlike in previous versions of this library, a Socket instance now represents a single underlying socket resource. It’s designed to be used by aggregation, rather than inheritence.

constant TIMEOUT_SOCKET

Default timeout for socket operations (reads, writes)

constant DEFAULT_RECEIVE_LENGTH
constant NAME_PART_IP

Socket name parts

property socket

protected resource

property context

protected

Stream context

property connected

protected boolean

Whether the socket is connected to a server

Note, the connection may not be ready to use, but the socket is connected at least. See $handshaked, and other properties in subclasses.

property firstRead

protected boolean

Whether the current read is the first one to the socket

property name

protected string

The socket name according to stream_socket_get_name

property options

protected array

property protocol

protected Protocol

configure($options)

Configure options

Options include - timeout_connect => int, seconds, default 2 - timeout_socket => int, seconds, default 5

Parameters:
  • $options (array) –
Returns:

void

getName()

Gets the name of the socket

getNamePart($name, $part)

Gets part of the name of the socket

PHP seems to return IPV6 address/port combos like this: ::1:1234, where ::1 is the address and 1234 the port So, the part number here is either the last : delimited section (the port) or all the other sections (the whole initial part, the address).

Parameters:
  • $name (string) – (from $this->getName() usually)
  • $part
Returns:

string

getIp()

Gets the IP address of the socket

Returns:string
getPort()

Gets the port of the socket

Returns:int
getLastError()

Get the last error that occurred on the socket

Returns:int|string
isConnected()

Whether the socket is currently connected

Returns:boolean
disconnect()

Disconnect the socket

Returns:void
getResource()
getResourceId()
send($data)
Parameters:
  • $data (unknown_type) –
Returns:

boolean|int The number of bytes sent or false on error

receive($length = self::DEFAULT_RECEIVE_LENGTH)

Recieve data from the socket

Parameters:
  • $length (int) –
Returns:

string

__construct($options = array())

Configurable constructor

Parameters:
  • $options
configureProtocol()

Configures the protocol option