@@ -18,7 +18,11 @@ handle multiple concurrent connections without blocking.
1818** Table of Contents**
1919
2020* [ Quickstart example] ( #quickstart-example )
21- * [ Usage] ( #usage )
21+ * [ Connection usage] ( #connection-usage )
22+ * [ ConnectionInterface] ( #connectioninterface )
23+ * [ getRemoteAddress()] ( #getremoteaddress )
24+ * [ getLocalAddress()] ( #getlocaladdress )
25+ * [ Server usage] ( #server-usage )
2226 * [ ServerInterface] ( #serverinterface )
2327 * [ connection event] ( #connection-event )
2428 * [ error event] ( #error-event )
@@ -30,18 +34,16 @@ handle multiple concurrent connections without blocking.
3034 * [ SecureServer] ( #secureserver )
3135 * [ LimitingServer] ( #limitingserver )
3236 * [ getConnections()] ( #getconnections )
33- * [ ConnectionInterface] ( #connectioninterface )
34- * [ getRemoteAddress()] ( #getremoteaddress )
35- * [ getLocalAddress()] ( #getlocaladdress )
37+ * [ Client usage] ( #client-usage )
3638 * [ ConnectorInterface] ( #connectorinterface )
3739 * [ connect()] ( #connect )
3840 * [ Connector] ( #connector )
39- * [ Advanced Usage ] ( #advanced-usage )
40- * [ TcpConnector] ( #tcpconnector )
41- * [ DnsConnector] ( #dnsconnector )
42- * [ SecureConnector] ( #secureconnector )
43- * [ TimeoutConnector] ( #timeoutconnector )
44- * [ UnixConnector] ( #unixconnector )
41+ * [ Advanced client usage ] ( #advanced-client -usage )
42+ * [ TcpConnector] ( #tcpconnector )
43+ * [ DnsConnector] ( #dnsconnector )
44+ * [ SecureConnector] ( #secureconnector )
45+ * [ TimeoutConnector] ( #timeoutconnector )
46+ * [ UnixConnector] ( #unixconnector )
4547* [ Install] ( #install )
4648* [ Tests] ( #tests )
4749* [ License] ( #license )
@@ -84,7 +86,102 @@ $connector->connect('127.0.0.1:8080')->then(function (ConnectionInterface $conn)
8486$loop->run();
8587```
8688
87- ## Usage
89+ ## Connection usage
90+
91+ ### ConnectionInterface
92+
93+ The ` ConnectionInterface ` is used to represent any incoming and outgoing
94+ connection, such as a normal TCP/IP connection.
95+
96+ An incoming or outgoing connection is a duplex stream (both readable and
97+ writable) that implements React's
98+ [ ` DuplexStreamInterface ` ] ( https://github.com/reactphp/stream#duplexstreaminterface ) .
99+ It contains additional properties for the local and remote address (client IP)
100+ where this connection has been established to/from.
101+
102+ Most commonly, instances implementing this ` ConnectionInterface ` are emitted
103+ by all classes implementing the [ ` ServerInterface ` ] ( #serverinterface ) and
104+ used by all classes implementing the [ ` ConnectorInterface ` ] ( #connectorinterface ) .
105+
106+ Because the ` ConnectionInterface ` implements the underlying
107+ [ ` DuplexStreamInterface ` ] ( https://github.com/reactphp/stream#duplexstreaminterface )
108+ you can use any of its events and methods as usual:
109+
110+ ``` php
111+ $connection->on('data', function ($chunk) {
112+ echo $chunk;
113+ });
114+
115+ $connection->on('end', function () {
116+ echo 'ended';
117+ });
118+
119+ $connection->on('error', function (Exception $e) {
120+ echo 'error: ' . $e->getMessage();
121+ });
122+
123+ $connection->on('close', function () {
124+ echo 'closed';
125+ });
126+
127+ $connection->write($data);
128+ $connection->end($data = null);
129+ $connection->close();
130+ // …
131+ ```
132+
133+ For more details, see the
134+ [ ` DuplexStreamInterface ` ] ( https://github.com/reactphp/stream#duplexstreaminterface ) .
135+
136+ #### getRemoteAddress()
137+
138+ The ` getRemoteAddress(): ?string ` method returns the full remote address
139+ (client IP and port) where this connection has been established with.
140+
141+ ``` php
142+ $address = $connection->getRemoteAddress();
143+ echo 'Connection with ' . $address . PHP_EOL;
144+ ```
145+
146+ If the remote address can not be determined or is unknown at this time (such as
147+ after the connection has been closed), it MAY return a ` NULL ` value instead.
148+
149+ Otherwise, it will return the full remote address as a string value.
150+ If this is a TCP/IP based connection and you only want the remote IP, you may
151+ use something like this:
152+
153+ ``` php
154+ $address = $connection->getRemoteAddress();
155+ $ip = trim(parse_url('tcp://' . $address, PHP_URL_HOST), '[]');
156+ echo 'Connection with ' . $ip . PHP_EOL;
157+ ```
158+
159+ #### getLocalAddress()
160+
161+ The ` getLocalAddress(): ?string ` method returns the full local address
162+ (client IP and port) where this connection has been established with.
163+
164+ ``` php
165+ $address = $connection->getLocalAddress();
166+ echo 'Connection with ' . $address . PHP_EOL;
167+ ```
168+
169+ If the local address can not be determined or is unknown at this time (such as
170+ after the connection has been closed), it MAY return a ` NULL ` value instead.
171+
172+ Otherwise, it will return the full local address as a string value.
173+
174+ This method complements the [ ` getRemoteAddress() ` ] ( #getremoteaddress ) method,
175+ so they should not be confused.
176+ If your ` Server ` instance is listening on multiple interfaces (e.g. using
177+ the address ` 0.0.0.0 ` ), you can use this method to find out which interface
178+ actually accepted this connection (such as a public or local interface).
179+
180+ If your system has multiple interfaces (e.g. a WAN and a LAN interface),
181+ you can use this method to find out which interface was actually
182+ used for this connection.
183+
184+ ## Server usage
88185
89186### ServerInterface
90187
@@ -473,98 +570,7 @@ foreach ($server->getConnection() as $connection) {
473570}
474571```
475572
476- ### ConnectionInterface
477-
478- The ` ConnectionInterface ` is used to represent any incoming and outgoing
479- connection, such as a normal TCP/IP connection.
480-
481- An incoming or outgoing connection is a duplex stream (both readable and
482- writable) that implements React's
483- [ ` DuplexStreamInterface ` ] ( https://github.com/reactphp/stream#duplexstreaminterface ) .
484- It contains additional properties for the local and remote address (client IP)
485- where this connection has been established to/from.
486-
487- Most commonly, instances implementing this ` ConnectionInterface ` are emitted
488- by all classes implementing the [ ` ServerInterface ` ] ( #serverinterface ) and
489- used by all classes implementing the [ ` ConnectorInterface ` ] ( #connectorinterface ) .
490-
491- Because the ` ConnectionInterface ` implements the underlying
492- [ ` DuplexStreamInterface ` ] ( https://github.com/reactphp/stream#duplexstreaminterface )
493- you can use any of its events and methods as usual:
494-
495- ``` php
496- $connection->on('data', function ($chunk) {
497- echo $chunk;
498- });
499-
500- $connection->on('end', function () {
501- echo 'ended';
502- });
503-
504- $connection->on('error', function (Exception $e) {
505- echo 'error: ' . $e->getMessage();
506- });
507-
508- $connection->on('close', function () {
509- echo 'closed';
510- });
511-
512- $connection->write($data);
513- $connection->end($data = null);
514- $connection->close();
515- // …
516- ```
517-
518- For more details, see the
519- [ ` DuplexStreamInterface ` ] ( https://github.com/reactphp/stream#duplexstreaminterface ) .
520-
521- #### getRemoteAddress()
522-
523- The ` getRemoteAddress(): ?string ` method returns the full remote address
524- (client IP and port) where this connection has been established with.
525-
526- ``` php
527- $address = $connection->getRemoteAddress();
528- echo 'Connection with ' . $address . PHP_EOL;
529- ```
530-
531- If the remote address can not be determined or is unknown at this time (such as
532- after the connection has been closed), it MAY return a ` NULL ` value instead.
533-
534- Otherwise, it will return the full remote address as a string value.
535- If this is a TCP/IP based connection and you only want the remote IP, you may
536- use something like this:
537-
538- ``` php
539- $address = $connection->getRemoteAddress();
540- $ip = trim(parse_url('tcp://' . $address, PHP_URL_HOST), '[]');
541- echo 'Connection with ' . $ip . PHP_EOL;
542- ```
543-
544- #### getLocalAddress()
545-
546- The ` getLocalAddress(): ?string ` method returns the full local address
547- (client IP and port) where this connection has been established with.
548-
549- ``` php
550- $address = $connection->getLocalAddress();
551- echo 'Connection with ' . $address . PHP_EOL;
552- ```
553-
554- If the local address can not be determined or is unknown at this time (such as
555- after the connection has been closed), it MAY return a ` NULL ` value instead.
556-
557- Otherwise, it will return the full local address as a string value.
558-
559- This method complements the [ ` getRemoteAddress() ` ] ( #getremoteaddress ) method,
560- so they should not be confused.
561- If your ` Server ` instance is listening on multiple interfaces (e.g. using
562- the address ` 0.0.0.0 ` ), you can use this method to find out which interface
563- actually accepted this connection (such as a public or local interface).
564-
565- If your system has multiple interfaces (e.g. a WAN and a LAN interface),
566- you can use this method to find out which interface was actually
567- used for this connection.
573+ ## Client usage
568574
569575### ConnectorInterface
570576
@@ -834,9 +840,9 @@ $connector->connect('google.com:80')->then(function (ConnectionInterface $connec
834840 Internally, the ` tcp:// ` and ` tls:// ` connectors will always be wrapped by
835841 ` TimeoutConnector ` , unless you disable timeouts like in the above example.
836842
837- ## Advanced Usage
843+ ### Advanced client usage
838844
839- ### TcpConnector
845+ #### TcpConnector
840846
841847The ` React\Socket\TcpConnector ` class implements the
842848[ ` ConnectorInterface ` ] ( #connectorinterface ) and allows you to create plaintext
@@ -895,7 +901,7 @@ be used to set up the TLS peer name.
895901This is used by the ` SecureConnector ` and ` DnsConnector ` to verify the peer
896902name and can also be used if you want a custom TLS peer name.
897903
898- ### DnsConnector
904+ #### DnsConnector
899905
900906The ` DnsConnector ` class implements the
901907[ ` ConnectorInterface ` ] ( #connectorinterface ) and allows you to create plaintext
@@ -945,7 +951,7 @@ hostname and is used by the `TcpConnector` to set up the TLS peer name.
945951If a ` hostname ` is given explicitly, this query parameter will not be modified,
946952which can be useful if you want a custom TLS peer name.
947953
948- ### SecureConnector
954+ #### SecureConnector
949955
950956The ` SecureConnector ` class implements the
951957[ ` ConnectorInterface ` ] ( #connectorinterface ) and allows you to create secure
@@ -999,7 +1005,7 @@ Failing to do so may result in a TLS peer name mismatch error or some hard to
9991005trace race conditions, because all stream resources will use a single, shared
10001006* default context* resource otherwise.
10011007
1002- ### TimeoutConnector
1008+ #### TimeoutConnector
10031009
10041010The ` TimeoutConnector ` class implements the
10051011[ ` ConnectorInterface ` ] ( #connectorinterface ) and allows you to add timeout
@@ -1030,7 +1036,7 @@ $promise->cancel();
10301036Calling ` cancel() ` on a pending promise will cancel the underlying connection
10311037attempt, abort the timer and reject the resulting promise.
10321038
1033- ### UnixConnector
1039+ #### UnixConnector
10341040
10351041The ` UnixConnector ` class implements the
10361042[ ` ConnectorInterface ` ] ( #connectorinterface ) and allows you to connect to
0 commit comments