@@ -30,10 +30,12 @@ handle multiple concurrent connections without blocking.
3030 * [ pause()] ( #pause )
3131 * [ resume()] ( #resume )
3232 * [ close()] ( #close )
33- * [ TcpServer] ( #tcpserver )
34- * [ SecureServer] ( #secureserver )
35- * [ LimitingServer] ( #limitingserver )
36- * [ getConnections()] ( #getconnections )
33+ * [ Server] ( #server )
34+ * [ Advanced server usage] ( #advanced-server-usage )
35+ * [ TcpServer] ( #tcpserver )
36+ * [ SecureServer] ( #secureserver )
37+ * [ LimitingServer] ( #limitingserver )
38+ * [ getConnections()] ( #getconnections )
3739* [ Client usage] ( #client-usage )
3840 * [ ConnectorInterface] ( #connectorinterface )
3941 * [ connect()] ( #connect )
@@ -54,8 +56,8 @@ Here is a server that closes the connection if you send it anything:
5456
5557``` php
5658$loop = React\EventLoop\Factory::create();
59+ $socket = new React\Socket\Server('127.0.0.1:8080', $loop);
5760
58- $socket = new React\Socket\TcpServer(8080, $loop);
5961$socket->on('connection', function (ConnectionInterface $conn) {
6062 $conn->write("Hello " . $conn->getRemoteAddress() . "!\n");
6163 $conn->write("Welcome to this amazing server!\n");
@@ -319,7 +321,149 @@ $server->close();
319321
320322Calling this method more than once on the same instance is a NO-OP.
321323
322- ### TcpServer
324+ ### Server
325+
326+ The ` Server ` class is the main class in this package that implements the
327+ [ ` ServerInterface ` ] ( #serverinterface ) and allows you to accept incoming
328+ streaming connections, such as plaintext TCP/IP or secure TLS connection streams.
329+
330+ ``` php
331+ $server = new Server(8080, $loop);
332+ ```
333+
334+ As above, the ` $uri ` parameter can consist of only a port, in which case the
335+ server will default to listening on the localhost address ` 127.0.0.1 ` ,
336+ which means it will not be reachable from outside of this system.
337+
338+ In order to use a random port assignment, you can use the port ` 0 ` :
339+
340+ ``` php
341+ $server = new Server(0, $loop);
342+ $address = $server->getAddress();
343+ ```
344+
345+ In order to change the host the socket is listening on, you can provide an IP
346+ address through the first parameter provided to the constructor, optionally
347+ preceded by the ` tcp:// ` scheme:
348+
349+ ``` php
350+ $server = new Server('192.168.0.1:8080', $loop);
351+ ```
352+
353+ If you want to listen on an IPv6 address, you MUST enclose the host in square
354+ brackets:
355+
356+ ``` php
357+ $server = new Server('[::1]:8080', $loop);
358+ ```
359+
360+ If the given URI is invalid, does not contain a port, any other scheme or if it
361+ contains a hostname, it will throw an ` InvalidArgumentException ` :
362+
363+ ``` php
364+ // throws InvalidArgumentException due to missing port
365+ $server = new Server('127.0.0.1', $loop);
366+ ```
367+
368+ If the given URI appears to be valid, but listening on it fails (such as if port
369+ is already in use or port below 1024 may require root access etc.), it will
370+ throw a ` RuntimeException ` :
371+
372+ ``` php
373+ $first = new Server(8080, $loop);
374+
375+ // throws RuntimeException because port is already in use
376+ $second = new Server(8080, $loop);
377+ ```
378+
379+ > Note that these error conditions may vary depending on your system and/or
380+ configuration.
381+ See the exception message and code for more details about the actual error
382+ condition.
383+
384+ Optionally, you can specify [ TCP socket context options] ( http://php.net/manual/en/context.socket.php )
385+ for the underlying stream socket resource like this:
386+
387+ ``` php
388+ $server = new Server('[::1]:8080', $loop, array(
389+ 'tcp' => array(
390+ 'backlog' => 200,
391+ 'so_reuseport' => true,
392+ 'ipv6_v6only' => true
393+ )
394+ ));
395+ ```
396+
397+ > Note that available [ socket context options] ( http://php.net/manual/en/context.socket.php ) ,
398+ their defaults and effects of changing these may vary depending on your system
399+ and/or PHP version.
400+ Passing unknown context options has no effect.
401+ For BC reasons, you can also pass the TCP socket context options as a simple
402+ array without wrapping this in another array under the ` tcp ` key.
403+
404+ You can start a secure TLS (formerly known as SSL) server by simply prepending
405+ the ` tls:// ` URI scheme.
406+ Internally, it will wait for plaintext TCP/IP connections and then performs a
407+ TLS handshake for each connection.
408+ It thus requires valid [ TLS context options] ( http://php.net/manual/en/context.ssl.php ) ,
409+ which in its most basic form may look something like this if you're using a
410+ PEM encoded certificate file:
411+
412+ ``` php
413+ $server = new Server('tls://127.0.0.1:8080', $loop, array(
414+ 'tls' => array(
415+ 'local_cert' => 'server.pem'
416+ )
417+ ));
418+ ```
419+
420+ > Note that the certificate file will not be loaded on instantiation but when an
421+ incoming connection initializes its TLS context.
422+ This implies that any invalid certificate file paths or contents will only cause
423+ an ` error ` event at a later time.
424+
425+ If your private key is encrypted with a passphrase, you have to specify it
426+ like this:
427+
428+ ``` php
429+ $server = new Server('tls://127.0.0.1:8000', $loop, array(
430+ 'tls' => array(
431+ 'local_cert' => 'server.pem',
432+ 'passphrase' => 'secret'
433+ )
434+ ));
435+ ```
436+
437+ > Note that available [ TLS context options] ( http://php.net/manual/en/context.ssl.php ) ,
438+ their defaults and effects of changing these may vary depending on your system
439+ and/or PHP version.
440+ The outer context array allows you to also use ` tcp ` (and possibly more)
441+ context options at the same time.
442+ Passing unknown context options has no effect.
443+ If you do not use the ` tls:// ` scheme, then passing ` tls ` context options
444+ has no effect.
445+
446+ Whenever a client connects, it will emit a ` connection ` event with a connection
447+ instance implementing [ ` ConnectionInterface ` ] ( #connectioninterface ) :
448+
449+ ``` php
450+ $server->on('connection', function (ConnectionInterface $connection) {
451+ echo 'Plaintext connection from ' . $connection->getRemoteAddress() . PHP_EOL;
452+
453+ $connection->write('hello there!' . PHP_EOL);
454+ …
455+ });
456+ ```
457+
458+ See also the [ ` ServerInterface ` ] ( #serverinterface ) for more details.
459+
460+ > Note that the ` Server ` class is a concrete implementation for TCP/IP sockets.
461+ If you want to typehint in your higher-level protocol implementation, you SHOULD
462+ use the generic [ ` ServerInterface ` ] ( #serverinterface ) instead.
463+
464+ ### Advanced server usage
465+
466+ #### TcpServer
323467
324468The ` TcpServer ` class implements the [ ` ServerInterface ` ] ( #serverinterface ) and
325469is responsible for accepting plaintext TCP/IP connections.
@@ -408,11 +552,7 @@ $server->on('connection', function (ConnectionInterface $connection) {
408552
409553See also the [ ` ServerInterface ` ] ( #serverinterface ) for more details.
410554
411- Note that the ` TcpServer ` class is a concrete implementation for TCP/IP sockets.
412- If you want to typehint in your higher-level protocol implementation, you SHOULD
413- use the generic [ ` ServerInterface ` ] ( #serverinterface ) instead.
414-
415- ### SecureServer
555+ #### SecureServer
416556
417557The ` SecureServer ` class implements the [ ` ServerInterface ` ] ( #serverinterface )
418558and is responsible for providing a secure TLS (formerly known as SSL) server.
@@ -492,7 +632,7 @@ If you use a custom `ServerInterface` and its `connection` event does not
492632meet this requirement, the ` SecureServer ` will emit an ` error ` event and
493633then close the underlying connection.
494634
495- ### LimitingServer
635+ #### LimitingServer
496636
497637The ` LimitingServer ` decorator wraps a given ` ServerInterface ` and is responsible
498638for limiting and keeping track of open connections to this server instance.
@@ -559,7 +699,7 @@ $server->on('connection', function (ConnectionInterface $connection) {
559699});
560700```
561701
562- #### getConnections()
702+ ##### getConnections()
563703
564704The ` getConnections(): ConnectionInterface[] ` method can be used to
565705return an array with all currently active connections.
0 commit comments