Overview

Namespaces

  • Evenement
  • None
  • PHP
  • Psr
    • Http
      • Message
  • Ratchet
    • Http
    • RFC6455
      • Handshake
      • Messaging
    • Server
    • Session
      • Serialize
      • Storage
        • Proxy
    • Wamp
    • WebSocket
  • React
    • EventLoop
      • Tick
      • Timer
    • Socket
    • Stream
  • Symfony
    • Component
      • HttpFoundation
        • Session
          • Attribute
          • Flash
          • Storage
            • Handler
            • Proxy
      • Routing
        • Annotation
        • Exception
        • Generator
          • Dumper
        • Loader
          • DependencyInjection
        • Matcher
          • Dumper
        • Tests
          • Annotation
          • Fixtures
            • AnnotatedClasses
            • OtherAnnotatedClasses
          • Generator
            • Dumper
          • Loader
          • Matcher
            • Dumper

Classes

  • Connector
  • DnsConnector
  • LimitingServer
  • SecureConnector
  • SecureServer
  • Server
  • TcpConnector
  • TimeoutConnector
  • UnixConnector

Interfaces

  • ConnectionInterface
  • ConnectorInterface
  • ServerInterface
  • Overview
  • Namespace
  • Class
  • Tree

Class SecureServer

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 Server instance which waits for plaintext TCP/IP connections and then performs a TLS handshake for each connection.

`php $server = new Server(8000, $loop); $server = new SecureServer($server, $loop, array( // tls context options here… )); `

Whenever a client completes the TLS handshake, it will emit a connection event with a connection instance implementing [ConnectionInterface](#connectioninterface):

`php $server->on('connection', function (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:

`php $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.

Evenement\EventEmitter implements Evenement\EventEmitterInterface uses Evenement\EventEmitterTrait
Extended by React\Socket\SecureServer implements React\Socket\ServerInterface
Final
Namespace: React\Socket
See: React\Socket\ServerInterface
See: React\Socket\ConnectionInterface
Located at SecureServer.php
Methods summary
public
# __construct( React\Socket\ServerInterface $tcp, React\EventLoop\LoopInterface $loop, array $context )

Creates a secure TLS server and starts waiting for incoming connections

Creates a secure TLS server and starts waiting for incoming connections

It does so by wrapping a Server 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:

`php $server = new Server(8000, $loop); $server = new SecureServer($server, $loop, 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:

`php $server = new Server(8000, $loop); $server = new SecureServer($server, $loop, 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.

Advanced usage: Despite allowing any ServerInterface as first parameter, you SHOULD pass a Server 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 React\Stream\Stream class. The Server class is guaranteed to emit connections that implement the ConnectionInterface and also extend the Stream 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
React\Socket\ServerInterface|React\Socket\Server
$tcp
$loop
React\EventLoop\LoopInterface
$loop
$context
array
$context

Throws

BadMethodCallException
for legacy HHVM < 3.8 due to lack of support

See

React\Socket\Server

Link

for TLS context options
public ?string
# getAddress( )

Returns the full address (IP and port) this server is currently listening on

Returns the full address (IP and port) this server is currently listening on

`php $address = $server->getAddress(); echo 'Server listening on ' . $address . PHP_EOL; `

It will return the full address (IP and port) or NULL if it is unknown (not applicable to this server socket or already closed).

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

`php $address = $server->getAddress(); $port = parse_url('tcp://' . $address, PHP_URL_PORT); echo 'Server listening on port ' . $port . PHP_EOL; `

Returns

?string
the full listening address (IP and port) or NULL if it is unknown (not applicable to this server socket or already closed)

Implementation of

React\Socket\ServerInterface::getAddress()
public
# pause( )

Pauses accepting new incoming connections.

Pauses accepting new incoming connections.

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.

`php $server->pause();

$server->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.

See

React\Socket\SecureServer::resume()

Implementation of

React\Socket\ServerInterface::pause()
public
# resume( )

Resumes accepting new incoming connections.

Resumes accepting new incoming connections.

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

`php $server->pause();

$loop->addTimer(1.0, function () use ($server) { $server->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.

See

React\Socket\SecureServer::pause()

Implementation of

React\Socket\ServerInterface::resume()
public
# close( )

Shuts down this listening socket

Shuts down this listening socket

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.

Implementation of

React\Socket\ServerInterface::close()
Methods inherited from Evenement\EventEmitterInterface
emit(), listeners(), on(), once(), removeAllListeners(), removeListener()
Methods used from Evenement\EventEmitterTrait
(), (), (), (), (), ()
Properties used from Evenement\EventEmitterTrait
$listeners
Ratchet API documentation generated by ApiGen 2.8.0