diff --git a/README.md b/README.md index 3bd024f8..68def71e 100644 --- a/README.md +++ b/README.md @@ -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). diff --git a/composer.json b/composer.json index 2a007dac..f04bf7cb 100644 --- a/composer.json +++ b/composer.json @@ -14,6 +14,11 @@ "React\\Socket\\": "src" } }, + "autoload-dev": { + "psr-4": { + "React\\Tests\\Socket\\": "tests" + } + }, "extra": { "branch-alias": { "dev-master": "0.4-dev" diff --git a/phpunit.xml.dist b/phpunit.xml.dist index cba6d4dd..13d3fab0 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -9,7 +9,7 @@ processIsolation="false" stopOnFailure="false" syntaxCheck="false" - bootstrap="tests/bootstrap.php" + bootstrap="vendor/autoload.php" > diff --git a/tests/bootstrap.php b/tests/bootstrap.php deleted file mode 100644 index e53e7e66..00000000 --- a/tests/bootstrap.php +++ /dev/null @@ -1,7 +0,0 @@ -addPsr4('React\\Tests\\Socket\\', __DIR__);