Constants

FIN

FIN = 128

MASK

MASK = 128

OPCODE_CONTINUATION

OPCODE_CONTINUATION = 0

OPCODE_TEXT

OPCODE_TEXT = 1

PAYLOAD_LENGTH_16

PAYLOAD_LENGTH_16 = 126

PAYLOAD_LENGTH_63

PAYLOAD_LENGTH_63 = 127

Properties

$host

$host : string

Host to bind to.

Type

string

$port

$port : integer

Port number where to bind to.

Type

integer

$clients

$clients : array<mixed,\PHP\WebSocket\SocketClient>

Array of connected clients.

Type

array<mixed,\PHP\WebSocket\SocketClient>

$socket

$socket : resource

The master socket acting as server.

Type

resource

$sockets

$sockets : array<mixed,resource>

Array of all connected sockets, includes the master.

Type

array<mixed,resource>

$listeners

$listeners : array<mixed,\PHP\WebSocket\SocketListener>

Array of socket listeners.

(应用逻辑实现在这里)

Type

array<mixed,\PHP\WebSocket\SocketListener>

Methods

__construct()

__construct(string  $host = '127.0.0.1') 

Constructor, sets host and port to bind to.

Parameters

string $host

Socket host to bind to, defaults to localhost

removeListener()

removeListener(\PHP\WebSocket\SocketListener  $listener) : boolean

Removes an existing socket listener.

Parameters

\PHP\WebSocket\SocketListener $listener

Listener to remove

Returns

boolean —

Was the listener removed, false if not found

getListeners()

getListeners() : array<mixed,\PHP\WebSocket\SocketListener>

Returns all added socket listeners.

Returns

array<mixed,\PHP\WebSocket\SocketListener>

getClients()

getClients() : array<mixed,\PHP\WebSocket\SocketClient>

Returns array of connected clients

Returns

array<mixed,\PHP\WebSocket\SocketClient> —

Array of connected clients

getClientCount()

getClientCount() : integer

Returns the number of connected clients

Returns

integer —

Number of clients

getLastError()

getLastError(resource  $socket = null) : \stdClass

Returns last socket error as an object.

The object is a basic stdClass with parameters:

  • code: the code of the error
  • message: translated error code as message

Parameters

resource $socket

Optional socket resource

Returns

\stdClass —

Error as stdClass instance with fields code and message

listen()

listen(  $port, integer  $maxConnections = SOMAXCONN) 

Starts the server by binding to a port

Parameters

$port
integer $maxConnections

Max number of incoming backlog connections

Throws

\Exception

If something goes wrong

send()

send(resource  $socket, mixed  $message, integer  $bufferSize = 4096) 

Sends a message to given socket

Parameters

resource $socket

Socket to send the message to

mixed $message

Message to send

integer $bufferSize

Buffer size to use

Throws

\Exception

If something goes wrong

log()

log(string  $message) 

Logs a message to console

Parameters

string $message

Message to log

run()

run() : void

Runs the server as an infinite loop

connectClient()

connectClient(resource  $socket) 

Connects a client by socket.

Creates a new instance of the SocketClient class and adds it to the list of clients. Also adds the socket to the list of sockets.

Parameters

resource $socket

Socket to use

disconnectClient()

disconnectClient(resource  $clientSocket) 

Disconnects a client by socket.

Parameters

resource $clientSocket

Socket to use

getClientBySocket()

getClientBySocket(resource  $socket) : \PHP\WebSocket\SocketClient

Returns client by socket reference.

Parameters

resource $socket

Socket resource

Returns

\PHP\WebSocket\SocketClient

The client on the socket or null if not found

loopTask()

loopTask() 

acceptSocket()

acceptSocket() 

handleRequest()

handleRequest(  $socket) 

Parameters

$socket

handle()

handle(  $socket) 

Parameters

$socket