@@ -13,7 +13,8 @@ and [`Stream`](https://github.com/reactphp/stream) components.
13
13
* [ Quickstart example] ( #quickstart-example )
14
14
* [ Usage] ( #usage )
15
15
* [ Server] ( #server )
16
- * [ Connection] ( #connection )
16
+ * [ ConnectionInterface] ( #connectioninterface )
17
+ * [ getRemoteAddress()] ( #getremoteaddress )
17
18
* [ Install] ( #install )
18
19
* [ License] ( #license )
19
20
@@ -25,8 +26,8 @@ Here is a server that closes the connection if you send it anything:
25
26
$loop = React\EventLoop\Factory::create();
26
27
27
28
$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");
30
31
$conn->write("Welcome to this amazing server!\n");
31
32
$conn->write("Here's a tip: don't say anything.\n");
32
33
@@ -68,18 +69,68 @@ $loop->run();
68
69
69
70
### Server
70
71
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.
73
73
74
- ### Connection
74
+ Whenever a client connects, it will emit a ` connection ` event with a connection
75
+ instance implementing [ ` ConnectionInterface ` ] ( #connectioninterface ) :
75
76
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.
78
92
93
+ > Note that this interface is only to be used to represent the server-side end
94
+ of an incoming connection.
79
95
It MUST NOT be used to represent an outgoing connection in a client-side context.
80
96
If you want to establish an outgoing connection,
81
97
use the [ ` SocketClient ` ] ( https://github.com/reactphp/socket-client ) component instead.
82
98
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
+
83
134
## Install
84
135
85
136
The recommended way to install this library is [ through Composer] ( http://getcomposer.org ) .
0 commit comments