@@ -13,7 +13,8 @@ and [`Stream`](https://github.com/reactphp/stream) components.
1313* [ Quickstart example] ( #quickstart-example )
1414* [ Usage] ( #usage )
1515 * [ Server] ( #server )
16- * [ Connection] ( #connection )
16+ * [ ConnectionInterface] ( #connectioninterface )
17+ * [ getRemoteAddress()] ( #getremoteaddress )
1718* [ Install] ( #install )
1819* [ License] ( #license )
1920
@@ -25,8 +26,8 @@ Here is a server that closes the connection if you send it anything:
2526$loop = React\EventLoop\Factory::create();
2627
2728$socket = new React\Socket\Server($loop);
28- $socket->on('connection', function ($conn) {
29- $conn->write("Hello there !\n");
29+ $socket->on('connection', function (ConnectionInterface $conn) {
30+ $conn->write("Hello " . $conn->getRemoteAddress() . " !\n");
3031 $conn->write("Welcome to this amazing server!\n");
3132 $conn->write("Here's a tip: don't say anything.\n");
3233
@@ -68,18 +69,68 @@ $loop->run();
6869
6970### Server
7071
71- The server can listen on a port and will emit a ` connection ` event whenever a
72- client connects.
72+ The ` Server ` class is responsible for listening on a port and waiting for new connections.
7373
74- ### Connection
74+ Whenever a client connects, it will emit a ` connection ` event with a connection
75+ instance implementing [ ` ConnectionInterface ` ] ( #connectioninterface ) :
7576
76- The ` Connection ` is a readable and writable [ ` Stream ` ] ( https://github.com/reactphp/stream ) .
77- The incoming connection represents the server-side end of the connection.
77+ ``` php
78+ $server->on('connection', function (ConnectionInterface $connection) {
79+ …
80+ });
81+ ```
82+
83+ ### ConnectionInterface
84+
85+ The ` ConnectionInterface ` is used to represent any incoming connection.
86+
87+ An incoming connection is a duplex stream (both readable and writable) that
88+ implements React's
89+ [ ` DuplexStreamInterface ` ] ( https://github.com/reactphp/stream#duplexstreaminterface )
90+ and contains only a single additional property, the remote address (client IP)
91+ where this connection has been established from.
7892
93+ > Note that this interface is only to be used to represent the server-side end
94+ of an incoming connection.
7995It MUST NOT be used to represent an outgoing connection in a client-side context.
8096If you want to establish an outgoing connection,
8197use the [ ` SocketClient ` ] ( https://github.com/reactphp/socket-client ) component instead.
8298
99+ Because the ` ConnectionInterface ` implements the underlying
100+ [ ` DuplexStreamInterface ` ] ( https://github.com/reactphp/stream#duplexstreaminterface )
101+ you can use any of its events and methods as usual:
102+
103+ ``` php
104+ $connection->on('data', function ($chunk) {
105+ echo $data;
106+ });
107+
108+ $conenction->on('close', function () {
109+ echo 'closed';
110+ });
111+
112+ $connection->write($data);
113+ $connection->end($data = null);
114+ $connection->close();
115+ // …
116+ ```
117+
118+ For more details, see the
119+ [ ` DuplexStreamInterface ` ] ( https://github.com/reactphp/stream#duplexstreaminterface ) .
120+
121+ #### getRemoteAddress()
122+
123+ The ` getRemoteAddress(): ?string ` method returns the remote address
124+ (client IP) where this connection has been established from.
125+
126+ ``` php
127+ $ip = $connection->getRemoteAddress();
128+ ```
129+
130+ It will return the remote address as a string value.
131+ If the remote address can not be determined or is unknown at this time (such as
132+ after the connection has been closed), it MAY return a ` NULL ` value instead.
133+
83134## Install
84135
85136The recommended way to install this library is [ through Composer] ( http://getcomposer.org ) .
0 commit comments