Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove branch-alias?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for spotting, didn't mean to include this in this PR. I've removed these changes and filed reactphp/reactphp#343 instead.

"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.