Skip to content

Commit bf7b31e

Browse files
authored
Merge pull request #50 from clue-labs/connectioninterface
ConnectionInterface should extend DuplexStreamInterface + documentation
2 parents 3fca60f + 5af5ee3 commit bf7b31e

File tree

4 files changed

+95
-13
lines changed

4 files changed

+95
-13
lines changed

README.md

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
7995
It MUST NOT be used to represent an outgoing connection in a client-side context.
8096
If you want to establish an outgoing connection,
8197
use 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

85136
The recommended way to install this library is [through Composer](http://getcomposer.org).

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"php": ">=5.3.0",
88
"evenement/evenement": "~2.0|~1.0",
99
"react/event-loop": "0.4.*|0.3.*",
10-
"react/stream": "0.4.*|0.3.*"
10+
"react/stream": "^0.4.2"
1111
},
1212
"autoload": {
1313
"psr-4": {

src/Connection.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44

55
use React\Stream\Stream;
66

7+
/**
8+
* The actual connection implementation for ConnectionInterface
9+
*
10+
* This class should only be used internally, see ConnectionInterface instead.
11+
*
12+
* @see ConnectionInterface
13+
* @internal
14+
*/
715
class Connection extends Stream implements ConnectionInterface
816
{
917
public function handleData($stream)

src/ConnectionInterface.php

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,34 @@
22

33
namespace React\Socket;
44

5-
use Evenement\EventEmitterInterface;
6-
use React\Stream\ReadableStreamInterface;
7-
use React\Stream\WritableStreamInterface;
5+
use React\Stream\DuplexStreamInterface;
86

9-
interface ConnectionInterface extends ReadableStreamInterface, WritableStreamInterface
7+
/**
8+
* Any incoming connection is represented by this interface.
9+
*
10+
* An incoming connection is a duplex stream (both readable and writable) that
11+
* implements React's DuplexStreamInterface and contains only a single
12+
* additional property, the remote address (client IP) where this connection has
13+
* been established from.
14+
*
15+
* Note that this interface is only to be used to represent the server-side end
16+
* of an incoming connection.
17+
* It MUST NOT be used to represent an outgoing connection in a client-side
18+
* context.
19+
* If you want to establish an outgoing connection,
20+
* use React's SocketClient component instead.
21+
*
22+
* Because the `ConnectionInterface` implements the underlying
23+
* `DuplexStreamInterface` you can use any of its events and methods as usual.
24+
*
25+
* @see DuplexStreamInterface
26+
*/
27+
interface ConnectionInterface extends DuplexStreamInterface
1028
{
29+
/**
30+
* Returns the remote address (client IP) where this connection has been established from
31+
*
32+
* @return string|null remote address (client IP) or null if unknown
33+
*/
1134
public function getRemoteAddress();
1235
}

0 commit comments

Comments
 (0)