@@ -15,7 +15,6 @@ and [`Stream`](https://github.com/reactphp/stream) components.
1515 * [ ServerInterface] ( #serverinterface )
1616 * [ connection event] ( #connection-event )
1717 * [ error event] ( #error-event )
18- * [ listen()] ( #listen )
1918 * [ getPort()] ( #getport )
2019 * [ shutdown()] ( #shutdown )
2120 * [ Server] ( #server )
@@ -33,7 +32,7 @@ Here is a server that closes the connection if you send it anything:
3332``` php
3433$loop = React\EventLoop\Factory::create();
3534
36- $socket = new React\Socket\Server($loop);
35+ $socket = new React\Socket\Server(8080, $loop);
3736$socket->on('connection', function (ConnectionInterface $conn) {
3837 $conn->write("Hello " . $conn->getRemoteAddress() . "!\n");
3938 $conn->write("Welcome to this amazing server!\n");
@@ -43,7 +42,6 @@ $socket->on('connection', function (ConnectionInterface $conn) {
4342 $conn->close();
4443 });
4544});
46- $socket->listen(1337);
4745
4846$loop->run();
4947```
@@ -58,7 +56,7 @@ For anything more complex, consider using the
5856``` php
5957$loop = React\EventLoop\Factory::create();
6058
61- $client = stream_socket_client('tcp://127.0.0.1:1337 ');
59+ $client = stream_socket_client('tcp://127.0.0.1:8080 ');
6260$conn = new React\Stream\Stream($client, $loop);
6361$conn->pipe(new React\Stream\Stream(STDOUT, $loop));
6462$conn->write("Hello World!\n");
@@ -112,28 +110,6 @@ $server->on('error', function (Exception $e) {
112110Note that this is not a fatal error event, i.e. the server keeps listening for
113111new connections even after this event.
114112
115- #### listen()
116-
117- The ` listen(int $port, string $host = '127.0.0.1'): void ` method can be used to
118- start listening on the given address.
119-
120- This starts accepting new incoming connections on the given address.
121- See also the [ connection event] ( #connection-event ) for more details.
122-
123- ``` php
124- $server->listen(8080);
125- ```
126-
127- By default, the server will listen on the localhost address and will not be
128- reachable from the outside.
129- You can change the host the socket is listening on through a second parameter
130- provided to the listen method:
131-
132- ``` php
133- $socket->listen(1337, '192.168.0.1');
134- ```
135-
136- This method MUST NOT be called more than once on the same instance.
137113
138114#### getPort()
139115
@@ -145,7 +121,6 @@ $port = $server->getPort();
145121echo 'Server listening on port ' . $port . PHP_EOL;
146122```
147123
148- This method MUST NOT be called before calling [ ` listen() ` ] ( #listen ) .
149124This method MUST NOT be called after calling [ ` shutdown() ` ] ( #shutdown ) .
150125
151126#### shutdown()
@@ -160,14 +135,26 @@ echo 'Shutting down server socket' . PHP_EOL;
160135$server->shutdown();
161136```
162137
163- This method MUST NOT be called before calling [ ` listen() ` ] ( #listen ) .
164- This method MUST NOT be called after calling [ ` shutdown() ` ] ( #shutdown ) .
138+ This method MUST NOT be called more than once on the same instance.
165139
166140### Server
167141
168142The ` Server ` class implements the [ ` ServerInterface ` ] ( #serverinterface ) and
169143is responsible for accepting plaintext TCP/IP connections.
170144
145+ ``` php
146+ $server = new Server(8080, $loop);
147+ ```
148+
149+ By default, the server will listen on the localhost address and will not be
150+ reachable from the outside.
151+ You can change the host the socket is listening on through a first parameter
152+ provided to the constructor:
153+
154+ ``` php
155+ $server = new Server('192.168.0.1:8080', $loop);
156+ ```
157+
171158Whenever a client connects, it will emit a ` connection ` event with a connection
172159instance implementing [ ` ConnectionInterface ` ] ( #connectioninterface ) :
173160
@@ -198,13 +185,10 @@ which in its most basic form may look something like this if you're using a
198185PEM encoded certificate file:
199186
200187``` php
201- $server = new Server($loop);
202-
188+ $server = new Server(8000, $loop);
203189$server = new SecureServer($server, $loop, array(
204190 'local_cert' => 'server.pem'
205191));
206-
207- $server->listen(8000);
208192```
209193
210194> Note that the certificate file will not be loaded on instantiation but when an
@@ -216,6 +200,7 @@ If your private key is encrypted with a passphrase, you have to specify it
216200like this:
217201
218202``` php
203+ $server = new Server(8000, $loop);
219204$server = new SecureServer($server, $loop, array(
220205 'local_cert' => 'server.pem',
221206 'passphrase' => 'secret'
0 commit comments