@@ -8,56 +8,95 @@ The socket component provides a more usable interface for a socket-layer
88server or client based on the [ ` EventLoop ` ] ( https://github.com/reactphp/event-loop )
99and [ ` Stream ` ] ( https://github.com/reactphp/stream ) components.
1010
11- ## Server
11+ ** Table of Contents **
1212
13- The server can listen on a port and will emit a ` connection ` event whenever a
14- client connects.
15-
16- ## Connection
17-
18- The ` Connection ` is a readable and writable [ ` Stream ` ] ( https://github.com/reactphp/stream ) .
19- The incoming connection represents the server-side end of the connection.
13+ * [ Quickstart example] ( #quickstart-example )
14+ * [ Usage] ( #usage )
15+ * [ Server] ( #server )
16+ * [ Connection] ( #connection )
17+ * [ Install] ( #install )
18+ * [ License] ( #license )
2019
21- It MUST NOT be used to represent an outgoing connection in a client-side context.
22- If you want to establish an outgoing connection,
23- use the [ ` SocketClient ` ] ( https://github.com/reactphp/socket-client ) component instead.
20+ ## Quickstart example
2421
25- ## Usage
22+ Here is a server that closes the connection if you send it anything:
2623
27- Here is a server that closes the connection if you send it anything.
2824``` php
29- $loop = React\EventLoop\Factory::create();
25+ $loop = React\EventLoop\Factory::create();
3026
31- $socket = new React\Socket\Server($loop);
32- $socket->on('connection', function ($conn) {
33- $conn->write("Hello there!\n");
34- $conn->write("Welcome to this amazing server!\n");
35- $conn->write("Here's a tip: don't say anything.\n");
27+ $socket = new React\Socket\Server($loop);
28+ $socket->on('connection', function ($conn) {
29+ $conn->write("Hello there!\n");
30+ $conn->write("Welcome to this amazing server!\n");
31+ $conn->write("Here's a tip: don't say anything.\n");
3632
37- $conn->on('data', function ($data) use ($conn) {
38- $conn->close();
39- });
33+ $conn->on('data', function ($data) use ($conn) {
34+ $conn->close();
4035 });
41- $socket->listen(1337);
36+ });
37+ $socket->listen(1337);
38+
39+ $loop->run();
40+ ```
4241
43- $loop->run();
44- ```
4542You can change the host the socket is listening on through a second parameter
4643provided to the listen method:
44+
4745``` php
48- $socket->listen(1337, '192.168.0.1');
46+ $socket->listen(1337, '192.168.0.1');
4947```
48+
5049Here's a client that outputs the output of said server and then attempts to
5150send it a string.
5251For anything more complex, consider using the
5352[ ` SocketClient ` ] ( https://github.com/reactphp/socket-client ) component instead.
53+
5454``` php
55- $loop = React\EventLoop\Factory::create();
55+ $loop = React\EventLoop\Factory::create();
56+
57+ $client = stream_socket_client('tcp://127.0.0.1:1337');
58+ $conn = new React\Stream\Stream($client, $loop);
59+ $conn->pipe(new React\Stream\Stream(STDOUT, $loop));
60+ $conn->write("Hello World!\n");
61+
62+ $loop->run();
63+ ```
64+
65+ ## Usage
5666
57- $client = stream_socket_client('tcp://127.0.0.1:1337');
58- $conn = new React\Stream\Stream($client, $loop);
59- $conn->pipe(new React\Stream\Stream(STDOUT, $loop));
60- $conn->write("Hello World!\n");
67+ ### Server
6168
62- $loop->run();
69+ The server can listen on a port and will emit a ` connection ` event whenever a
70+ client connects.
71+
72+ ### Connection
73+
74+ The ` Connection ` is a readable and writable [ ` Stream ` ] ( https://github.com/reactphp/stream ) .
75+ The incoming connection represents the server-side end of the connection.
76+
77+ It MUST NOT be used to represent an outgoing connection in a client-side context.
78+ If you want to establish an outgoing connection,
79+ use the [ ` SocketClient ` ] ( https://github.com/reactphp/socket-client ) component instead.
80+
81+ ## Install
82+
83+ The recommended way to install this library is [ through Composer] ( http://getcomposer.org ) .
84+ [ New to Composer?] ( http://getcomposer.org/doc/00-intro.md )
85+
86+ This will install the latest supported version:
87+
88+ ``` bash
89+ $ composer require react/socket:~ 0.4.0
90+ ```
91+
92+ If you care a lot about BC, you may also want to look into supporting legacy versions:
93+
94+ ``` bash
95+ $ composer require " react/socket:~0.4.0|~0.3.0"
6396```
97+
98+ More details and upgrade guides can be found in the [ CHANGELOG] ( CHANGELOG.md ) .
99+
100+ ## License
101+
102+ MIT, see [ LICENSE file] ( LICENSE ) .
0 commit comments