PHP Class Resque\Socket\Server

Author: Michael Haynes ([email protected])
Datei anzeigen Open project: mjphaynes/php-resque Class Usage Examples

Protected Properties

Property Type Description
$clients The connected clients.
$config Configuration information used by the server.
$events Dictionary of events and the callbacks attached to them.
$logger Monolog logger interface
$max_clients The maximum number of clients allowed to connect.
$max_read The maximum number of bytes to read from a socket at a single time.
$shutdown True if on the next iteration, the server should shutdown.
$socket The socket used by the server.
$started if the server has started
$tv_sec Connection timeout

Public Methods

Method Description
__construct ( array $config, Logger $logger ) Creates the socket and starts listening to it.
__toString ( ) : string Generate a string representation of this server.
close ( ) Closes the socket on shutdown
disconnect ( Client $client, string $message = 'Goodbye.' ) : mixed Disconnect a client
fire ( string $event, Client &$client, mixed $data = null ) : true Raise a given event with the supplied data.
forget ( string $event, mixed $callback ) : true Deletes a function from the call list for a certain action
fwrite ( resource $fh, string $string ) Write a string to a resource
listen ( string $event, mixed $callback ) : true Adds a function to be called whenever a certain action happens
log ( ) Send log message to logger
onConnect ( mixed $callback ) : mixed Helper function to make using connect event easier
onDisconnect ( mixed $callback ) : mixed Helper function to make using disconnect event easier
onReceive ( mixed $callback ) : mixed Helper function to make using receive event easier
run ( ) Runs the server code until the server is shut down.
send ( Client &$client, string $message, string $end = true ) : mixed Writes data to the socket, including the length of the data, and ends it with a CRLF unless specified.
shutdown ( ) Schedule a shutdown. Will finish processing the current run.
start ( ) Starts the server

Method Details

__construct() public method

Creates the socket and starts listening to it.
public __construct ( array $config, Logger $logger )
$config array Array of configuration options
$logger Resque\Logger Output logger

__toString() public method

Generate a string representation of this server.
public __toString ( ) : string
return string String identifier for this server instance.

close() public method

Closes the socket on shutdown
public close ( )

disconnect() public method

Disconnect a client
public disconnect ( Client $client, string $message = 'Goodbye.' ) : mixed
$client Client The client to disconnect
$message string Data to write to the socket.
return mixed

fire() public method

Raise a given event with the supplied data.
public fire ( string $event, Client &$client, mixed $data = null ) : true
$event string Name of event to be raised.
$client Client Connected client
$data mixed Optional, any data that should be passed to each callback.
return true

forget() public method

Deletes a function from the call list for a certain action
public forget ( string $event, mixed $callback ) : true
$event string Name of event.
$callback mixed The callback as defined when listen() was called.
return true

fwrite() public static method

Write a string to a resource
public static fwrite ( resource $fh, string $string )
$fh resource The resource to write to
$string string The string to write

listen() public method

Adds a function to be called whenever a certain action happens
public listen ( string $event, mixed $callback ) : true
$event string Name of event to listen on.
$callback mixed Any callback callable by call_user_func_array.
return true

log() public method

Send log message to logger
public log ( )

onConnect() public method

Helper function to make using connect event easier
public onConnect ( mixed $callback ) : mixed
$callback mixed Any callback callable by call_user_func_array.
return mixed

onDisconnect() public method

Helper function to make using disconnect event easier
public onDisconnect ( mixed $callback ) : mixed
$callback mixed Any callback callable by call_user_func_array.
return mixed

onReceive() public method

Helper function to make using receive event easier
public onReceive ( mixed $callback ) : mixed
$callback mixed Any callback callable by call_user_func_array.
return mixed

run() public method

Runs the server code until the server is shut down.
public run ( )

send() public method

It is perfectly valid for socket_write to return zero which means no bytes have been written. Be sure to use the === operator to check for FALSE in case of an error.
public send ( Client &$client, string $message, string $end = true ) : mixed
$client Client Connected client to write to
$message string Data to write to the socket.
$end string Data to end the line with. Specify false if you don't want a line end sent.
return mixed Returns the number of bytes successfully written to the socket or FALSE on failure. The error code can be retrieved with socket_last_error(). This code may be passed to socket_strerror() to get a textual explanation of the error.

shutdown() public method

Schedule a shutdown. Will finish processing the current run.
public shutdown ( )

start() public method

Starts the server
public start ( )

Property Details

$clients protected_oe property

The connected clients.
protected $clients

$config protected_oe property

Configuration information used by the server.
protected $config

$events protected_oe property

Dictionary of events and the callbacks attached to them.
protected $events

$logger protected_oe property

Monolog logger interface
protected $logger

$max_clients protected_oe property

The maximum number of clients allowed to connect.
protected $max_clients

$max_read protected_oe property

The maximum number of bytes to read from a socket at a single time.
protected $max_read

$shutdown protected_oe property

True if on the next iteration, the server should shutdown.
protected $shutdown

$socket protected_oe property

The socket used by the server.
protected $socket

$started protected_oe property

if the server has started
protected $started

$tv_sec protected_oe property

Connection timeout
protected $tv_sec