Documentation

SecureServer extends EventEmitter
in package
implements ServerInterface

FinalYes

The `SecureServer` class implements the `ServerInterface` and is responsible for providing a secure TLS (formerly known as SSL) server.

It does so by wrapping a TcpServer instance which waits for plaintext TCP/IP connections and then performs a TLS handshake for each connection.

$server = new React\Socket\TcpServer(8000);
$server = new React\Socket\SecureServer($server, null, array(
    // tls context options here…
));

Whenever a client completes the TLS handshake, it will emit a connection event with a connection instance implementing ConnectionInterface:

$server->on('connection', function (React\Socket\ConnectionInterface $connection) {
    echo 'Secure connection from' . $connection->getRemoteAddress() . PHP_EOL;

    $connection->write('hello there!' . PHP_EOL);
    …
});

Whenever a client fails to perform a successful TLS handshake, it will emit an error event and then close the underlying TCP/IP connection:

$server->on('error', function (Exception $e) {
    echo 'Error' . $e->getMessage() . PHP_EOL;
});

See also the ServerInterface for more details.

Note that the SecureServer class is a concrete implementation for TLS sockets. If you want to typehint in your higher-level protocol implementation, you SHOULD use the generic ServerInterface instead.

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
$context  : mixed
$encryption  : mixed
$tcp  : mixed

Methods

__construct()  : mixed
Creates a secure TLS server and starts waiting for incoming connections
close()  : void
Shuts down this listening socket
emit()  : mixed
getAddress()  : string|null
Returns the full address (URI) this server is currently listening on
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()

Creates a secure TLS server and starts waiting for incoming connections

public __construct(ServerInterface|TcpServer $tcp[, LoopInterface|null $loop = null ][, array<string|int, mixed> $context = array() ]) : mixed

It does so by wrapping a TcpServer instance which waits for plaintext TCP/IP connections and then performs a TLS handshake for each connection. It thus requires valid [TLS context options], which in its most basic form may look something like this if you're using a PEM encoded certificate file:

$server = new React\Socket\TcpServer(8000);
$server = new React\Socket\SecureServer($server, null, array(
    'local_cert' => 'server.pem'
));

Note that the certificate file will not be loaded on instantiation but when an incoming connection initializes its TLS context. This implies that any invalid certificate file paths or contents will only cause an error event at a later time.

If your private key is encrypted with a passphrase, you have to specify it like this:

$server = new React\Socket\TcpServer(8000);
$server = new React\Socket\SecureServer($server, null, array(
    'local_cert' => 'server.pem',
    'passphrase' => 'secret'
));

Note that available [TLS context options], their defaults and effects of changing these may vary depending on your system and/or PHP version. Passing unknown context options has no effect.

This class takes an optional LoopInterface|null $loop parameter that can be used to pass the event loop instance to use for this object. You can use a null value here in order to use the default loop. This value SHOULD NOT be given unless you're sure you want to explicitly use a given event loop instance.

Advanced usage: Despite allowing any ServerInterface as first parameter, you SHOULD pass a TcpServer instance as first parameter, unless you know what you're doing. Internally, the SecureServer has to set the required TLS context options on the underlying stream resources. These resources are not exposed through any of the interfaces defined in this package, but only through the internal Connection class. The TcpServer class is guaranteed to emit connections that implement the ConnectionInterface and uses the internal Connection class in order to expose these underlying resources. If you use a custom ServerInterface and its connection event does not meet this requirement, the SecureServer will emit an error event and then close the underlying connection.

Parameters
$tcp : ServerInterface|TcpServer
$loop : LoopInterface|null = null
$context : array<string|int, mixed> = array()
Tags
throws
BadMethodCallException

for legacy HHVM < 3.8 due to lack of support

see
TcpServer
link

for TLS context options

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)

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