Documentation

LimitingServer extends EventEmitter
in package
implements ServerInterface

The `LimitingServer` decorator wraps a given `ServerInterface` and is responsible for limiting and keeping track of open connections to this server instance.

Whenever the underlying server emits a connection event, it will check its limits and then either

  • keep track of this connection by adding it to the list of open connections and then forward the connection event
  • or reject (close) the connection when its limits are exceeded and will forward an error event instead.

Whenever a connection closes, it will remove this connection from the list of open connections.

$server = new React\Socket\LimitingServer($server, 100);
$server->on('connection', function (React\Socket\ConnectionInterface $connection) {
    $connection->write('hello there!' . PHP_EOL);
    …
});

See also the ServerInterface for more details.

Tags
see
ServerInterface
see
ConnectionInterface

Table of Contents

Interfaces

ServerInterface
The `ServerInterface` is responsible for providing an interface for accepting incoming streaming connections, such as a normal TCP/IP connection.

Properties

$listeners  : mixed
$onceListeners  : mixed
$autoPaused  : mixed
$connections  : mixed
$limit  : mixed
$manuPaused  : mixed
$pauseOnLimit  : mixed
$server  : mixed

Methods

__construct()  : mixed
Instantiates a new LimitingServer.
close()  : void
Shuts down this listening socket
emit()  : mixed
getAddress()  : string|null
Returns the full address (URI) this server is currently listening on
getConnections()  : array<string|int, ConnectionInterface>
Returns an array with all currently active connections
listeners()  : array<string|int, mixed>
on()  : mixed
once()  : mixed
pause()  : void
Pauses accepting new incoming connections.
removeAllListeners()  : mixed
removeListener()  : mixed
resume()  : void
Resumes accepting new incoming connections.

Properties

Methods

__construct()

Instantiates a new LimitingServer.

public __construct(ServerInterface $server, int|null $connectionLimit[, bool $pauseOnLimit = false ]) : mixed

You have to pass a maximum number of open connections to ensure the server will automatically reject (close) connections once this limit is exceeded. In this case, it will emit an error event to inform about this and no connection event will be emitted.

$server = new React\Socket\LimitingServer($server, 100);
$server->on('connection', function (React\Socket\ConnectionInterface $connection) {
    $connection->write('hello there!' . PHP_EOL);
    …
});

You MAY pass a null limit in order to put no limit on the number of open connections and keep accepting new connection until you run out of operating system resources (such as open file handles). This may be useful if you do not want to take care of applying a limit but still want to use the getConnections() method.

You can optionally configure the server to pause accepting new connections once the connection limit is reached. In this case, it will pause the underlying server and no longer process any new connections at all, thus also no longer closing any excessive connections. The underlying operating system is responsible for keeping a backlog of pending connections until its limit is reached, at which point it will start rejecting further connections. Once the server is below the connection limit, it will continue consuming connections from the backlog and will process any outstanding data on each connection. This mode may be useful for some protocols that are designed to wait for a response message (such as HTTP), but may be less useful for other protocols that demand immediate responses (such as a "welcome" message in an interactive chat).

$server = new React\Socket\LimitingServer($server, 100, true);
$server->on('connection', function (React\Socket\ConnectionInterface $connection) {
    $connection->write('hello there!' . PHP_EOL);
    …
});
Parameters
$server : ServerInterface
$connectionLimit : int|null
$pauseOnLimit : bool = false

close()

Shuts down this listening socket

public close() : void

This will stop listening for new incoming connections on this socket.

Calling this method more than once on the same instance is a NO-OP.

emit()

public emit(mixed $event[, array<string|int, mixed> $arguments = [] ]) : mixed
Parameters
$event : mixed
$arguments : array<string|int, mixed> = []

getAddress()

Returns the full address (URI) this server is currently listening on

public getAddress() : string|null
$address = $socket->getAddress();
echo 'Server listening on ' . $address . PHP_EOL;

If the address can not be determined or is unknown at this time (such as after the socket has been closed), it MAY return a NULL value instead.

Otherwise, it will return the full address (URI) as a string value, such as tcp://127.0.0.1:8080, tcp://[::1]:80 or tls://127.0.0.1:443. Note that individual URI components are application specific and depend on the underlying transport protocol.

If this is a TCP/IP based server and you only want the local port, you may use something like this:

$address = $socket->getAddress();
$port = parse_url($address, PHP_URL_PORT);
echo 'Server listening on port ' . $port . PHP_EOL;
Return values
string|null

the full listening address (URI) or NULL if it is unknown (not applicable to this server socket or already closed)

getConnections()

Returns an array with all currently active connections

public getConnections() : array<string|int, ConnectionInterface>
foreach ($server->getConnection() as $connection) {
    $connection->write('Hi!');
}
Return values
array<string|int, ConnectionInterface>

listeners()

public listeners([mixed $event = null ]) : array<string|int, mixed>
Parameters
$event : mixed = null
Return values
array<string|int, mixed>

on()

public on(mixed $event, callable $listener) : mixed
Parameters
$event : mixed
$listener : callable

once()

public once(mixed $event, callable $listener) : mixed
Parameters
$event : mixed
$listener : callable

pause()

Pauses accepting new incoming connections.

public pause() : void

Removes the socket resource from the EventLoop and thus stop accepting new connections. Note that the listening socket stays active and is not closed.

This means that new incoming connections will stay pending in the operating system backlog until its configurable backlog is filled. Once the backlog is filled, the operating system may reject further incoming connections until the backlog is drained again by resuming to accept new connections.

Once the server is paused, no futher connection events SHOULD be emitted.

$socket->pause();

$socket->on('connection', assertShouldNeverCalled());

This method is advisory-only, though generally not recommended, the server MAY continue emitting connection events.

Unless otherwise noted, a successfully opened server SHOULD NOT start in paused state.

You can continue processing events by calling resume() again.

Note that both methods can be called any number of times, in particular calling pause() more than once SHOULD NOT have any effect. Similarly, calling this after close() is a NO-OP.

removeAllListeners()

public removeAllListeners([mixed $event = null ]) : mixed
Parameters
$event : mixed = null

removeListener()

public removeListener(mixed $event, callable $listener) : mixed
Parameters
$event : mixed
$listener : callable

resume()

Resumes accepting new incoming connections.

public resume() : void

Re-attach the socket resource to the EventLoop after a previous pause().

$socket->pause();

Loop::addTimer(1.0, function () use ($socket) {
    $socket->resume();
});

Note that both methods can be called any number of times, in particular calling resume() without a prior pause() SHOULD NOT have any effect. Similarly, calling this after close() is a NO-OP.


        
On this page

Search results