Skip to content

Commit

Permalink
Merge pull request #32 from clue-labs/doc
Browse files Browse the repository at this point in the history
Update documentation
  • Loading branch information
cboden committed Mar 1, 2016
2 parents d1fec4f + 741733e commit 795fae0
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 40 deletions.
103 changes: 71 additions & 32 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,56 +8,95 @@ The socket component provides a more usable interface for a socket-layer
server or client based on the [`EventLoop`](https://github.com/reactphp/event-loop)
and [`Stream`](https://github.com/reactphp/stream) components.

## Server
**Table of Contents**

The server can listen on a port and will emit a `connection` event whenever a
client connects.

## Connection

The `Connection` is a readable and writable [`Stream`](https://github.com/reactphp/stream).
The incoming connection represents the server-side end of the connection.
* [Quickstart example](#quickstart-example)
* [Usage](#usage)
* [Server](#server)
* [Connection](#connection)
* [Install](#install)
* [License](#license)

It MUST NOT be used to represent an outgoing connection in a client-side context.
If you want to establish an outgoing connection,
use the [`SocketClient`](https://github.com/reactphp/socket-client) component instead.
## Quickstart example

## Usage
Here is a server that closes the connection if you send it anything:

Here is a server that closes the connection if you send it anything.
```php
$loop = React\EventLoop\Factory::create();
$loop = React\EventLoop\Factory::create();

$socket = new React\Socket\Server($loop);
$socket->on('connection', function ($conn) {
$conn->write("Hello there!\n");
$conn->write("Welcome to this amazing server!\n");
$conn->write("Here's a tip: don't say anything.\n");
$socket = new React\Socket\Server($loop);
$socket->on('connection', function ($conn) {
$conn->write("Hello there!\n");
$conn->write("Welcome to this amazing server!\n");
$conn->write("Here's a tip: don't say anything.\n");

$conn->on('data', function ($data) use ($conn) {
$conn->close();
});
$conn->on('data', function ($data) use ($conn) {
$conn->close();
});
$socket->listen(1337);
});
$socket->listen(1337);

$loop->run();
```

$loop->run();
```
You can change the host the socket is listening on through a second parameter
provided to the listen method:

```php
$socket->listen(1337, '192.168.0.1');
$socket->listen(1337, '192.168.0.1');
```

Here's a client that outputs the output of said server and then attempts to
send it a string.
For anything more complex, consider using the
[`SocketClient`](https://github.com/reactphp/socket-client) component instead.

```php
$loop = React\EventLoop\Factory::create();
$loop = React\EventLoop\Factory::create();

$client = stream_socket_client('tcp://127.0.0.1:1337');
$conn = new React\Stream\Stream($client, $loop);
$conn->pipe(new React\Stream\Stream(STDOUT, $loop));
$conn->write("Hello World!\n");

$loop->run();
```

## Usage

$client = stream_socket_client('tcp://127.0.0.1:1337');
$conn = new React\Stream\Stream($client, $loop);
$conn->pipe(new React\Stream\Stream(STDOUT, $loop));
$conn->write("Hello World!\n");
### Server

$loop->run();
The server can listen on a port and will emit a `connection` event whenever a
client connects.

### Connection

The `Connection` is a readable and writable [`Stream`](https://github.com/reactphp/stream).
The incoming connection represents the server-side end of the connection.

It MUST NOT be used to represent an outgoing connection in a client-side context.
If you want to establish an outgoing connection,
use the [`SocketClient`](https://github.com/reactphp/socket-client) component instead.

## Install

The recommended way to install this library is [through Composer](http://getcomposer.org).
[New to Composer?](http://getcomposer.org/doc/00-intro.md)

This will install the latest supported version:

```bash
$ composer require react/socket:~0.4.0
```

If you care a lot about BC, you may also want to look into supporting legacy versions:

```bash
$ composer require "react/socket:~0.4.0|~0.3.0"
```

More details and upgrade guides can be found in the [CHANGELOG](CHANGELOG.md).

## License

MIT, see [LICENSE file](LICENSE).
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
"React\\Socket\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"React\\Tests\\Socket\\": "tests"
}
},
"extra": {
"branch-alias": {
"dev-master": "0.4-dev"
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="tests/bootstrap.php"
bootstrap="vendor/autoload.php"
>
<testsuites>
<testsuite name="React Test Suite">
Expand Down
7 changes: 0 additions & 7 deletions tests/bootstrap.php

This file was deleted.

0 comments on commit 795fae0

Please sign in to comment.