@@ -30,7 +30,7 @@ handle multiple concurrent connections without blocking.
30
30
* [ pause()] ( #pause )
31
31
* [ resume()] ( #resume )
32
32
* [ close()] ( #close )
33
- * [ Server ] ( #server )
33
+ * [ SocketServer ] ( #socketserver )
34
34
* [ Advanced server usage] ( #advanced-server-usage )
35
35
* [ TcpServer] ( #tcpserver )
36
36
* [ SecureServer] ( #secureserver )
@@ -58,7 +58,7 @@ handle multiple concurrent connections without blocking.
58
58
Here is a server that closes the connection if you send it anything:
59
59
60
60
``` php
61
- $socket = new React\Socket\Server ('127.0.0.1:8080');
61
+ $socket = new React\Socket\SocketServer ('127.0.0.1:8080');
62
62
63
63
$socket->on('connection', function (React\Socket\ConnectionInterface $connection) {
64
64
$connection->write("Hello " . $connection->getRemoteAddress() . "!\n");
@@ -214,7 +214,7 @@ The `connection` event will be emitted whenever a new connection has been
214
214
established, i.e. a new client connects to this server socket:
215
215
216
216
``` php
217
- $server ->on('connection', function (React\Socket\ConnectionInterface $connection) {
217
+ $socket ->on('connection', function (React\Socket\ConnectionInterface $connection) {
218
218
echo 'new connection' . PHP_EOL;
219
219
});
220
220
```
@@ -228,7 +228,7 @@ The `error` event will be emitted whenever there's an error accepting a new
228
228
connection from a client.
229
229
230
230
``` php
231
- $server ->on('error', function (Exception $e) {
231
+ $socket ->on('error', function (Exception $e) {
232
232
echo 'error: ' . $e->getMessage() . PHP_EOL;
233
233
});
234
234
```
@@ -243,7 +243,7 @@ The `getAddress(): ?string` method can be used to
243
243
return the full address (URI) this server is currently listening on.
244
244
245
245
``` php
246
- $address = $server ->getAddress();
246
+ $address = $socket ->getAddress();
247
247
echo 'Server listening on ' . $address . PHP_EOL;
248
248
```
249
249
@@ -260,7 +260,7 @@ If this is a TCP/IP based server and you only want the local port, you may
260
260
use something like this:
261
261
262
262
``` php
263
- $address = $server ->getAddress();
263
+ $address = $socket ->getAddress();
264
264
$port = parse_url($address, PHP_URL_PORT);
265
265
echo 'Server listening on port ' . $port . PHP_EOL;
266
266
```
@@ -284,9 +284,9 @@ Once the server is paused, no futher `connection` events SHOULD
284
284
be emitted.
285
285
286
286
``` php
287
- $server ->pause();
287
+ $socket ->pause();
288
288
289
- $server ->on('connection', assertShouldNeverCalled());
289
+ $socket ->on('connection', assertShouldNeverCalled());
290
290
```
291
291
292
292
This method is advisory-only, though generally not recommended, the
@@ -309,10 +309,10 @@ resume accepting new incoming connections.
309
309
Re-attach the socket resource to the EventLoop after a previous ` pause() ` .
310
310
311
311
``` php
312
- $server ->pause();
312
+ $socket ->pause();
313
313
314
- Loop::addTimer(1.0, function () use ($server ) {
315
- $server ->resume();
314
+ Loop::addTimer(1.0, function () use ($socket ) {
315
+ $socket ->resume();
316
316
});
317
317
```
318
318
@@ -329,90 +329,86 @@ This will stop listening for new incoming connections on this socket.
329
329
330
330
``` php
331
331
echo 'Shutting down server socket' . PHP_EOL;
332
- $server ->close();
332
+ $socket ->close();
333
333
```
334
334
335
335
Calling this method more than once on the same instance is a NO-OP.
336
336
337
- ### Server
337
+ ### SocketServer
338
338
339
- The ` Server ` class is the main class in this package that implements the
339
+ <a id =" server " ></a > <!-- legacy id -->
340
+
341
+ The ` SocketServer ` class is the main class in this package that implements the
340
342
[ ` ServerInterface ` ] ( #serverinterface ) and allows you to accept incoming
341
343
streaming connections, such as plaintext TCP/IP or secure TLS connection streams.
342
- Connections can also be accepted on Unix domain sockets.
343
344
344
- ``` php
345
- $server = new React\Socket\Server(8080);
346
- ```
347
-
348
- As above, the ` $uri ` parameter can consist of only a port, in which case the
349
- server will default to listening on the localhost address ` 127.0.0.1 ` ,
350
- which means it will not be reachable from outside of this system.
351
-
352
- In order to use a random port assignment, you can use the port ` 0 ` :
345
+ In order to accept plaintext TCP/IP connections, you can simply pass a host
346
+ and port combination like this:
353
347
354
348
``` php
355
- $server = new React\Socket\Server(0);
356
- $address = $server->getAddress();
349
+ $socket = new React\Socket\SocketServer('127.0.0.1:8080');
357
350
```
358
351
352
+ Listening on the localhost address ` 127.0.0.1 ` means it will not be reachable from
353
+ outside of this system.
359
354
In order to change the host the socket is listening on, you can provide an IP
360
- address through the first parameter provided to the constructor, optionally
361
- preceded by the ` tcp:// ` scheme :
355
+ address of an interface or use the special ` 0.0.0.0 ` address to listen on all
356
+ interfaces :
362
357
363
358
``` php
364
- $server = new React\Socket\Server('192.168 .0.1 :8080');
359
+ $socket = new React\Socket\SocketServer('0.0 .0.0 :8080');
365
360
```
366
361
367
362
If you want to listen on an IPv6 address, you MUST enclose the host in square
368
363
brackets:
369
364
370
365
``` php
371
- $server = new React\Socket\Server('[::1]:8080');
366
+ $socket = new React\Socket\SocketServer('[::1]:8080');
367
+ ```
368
+
369
+ In order to use a random port assignment, you can use the port ` 0 ` :
370
+
371
+ ``` php
372
+ $socket = new React\Socket\SocketServer('127.0.0.1:0');
373
+ $address = $socket->getAddress();
372
374
```
373
375
374
376
To listen on a Unix domain socket (UDS) path, you MUST prefix the URI with the
375
377
` unix:// ` scheme:
376
378
377
379
``` php
378
- $server = new React\Socket\Server ('unix:///tmp/server.sock');
380
+ $socket = new React\Socket\SocketServer ('unix:///tmp/server.sock');
379
381
```
380
382
381
383
If the given URI is invalid, does not contain a port, any other scheme or if it
382
384
contains a hostname, it will throw an ` InvalidArgumentException ` :
383
385
384
386
``` php
385
387
// throws InvalidArgumentException due to missing port
386
- $server = new React\Socket\Server ('127.0.0.1');
388
+ $socket = new React\Socket\SocketServer ('127.0.0.1');
387
389
```
388
390
389
391
If the given URI appears to be valid, but listening on it fails (such as if port
390
392
is already in use or port below 1024 may require root access etc.), it will
391
393
throw a ` RuntimeException ` :
392
394
393
395
``` php
394
- $first = new React\Socket\Server( 8080);
396
+ $first = new React\Socket\SocketServer('127.0.0.1: 8080' );
395
397
396
398
// throws RuntimeException because port is already in use
397
- $second = new React\Socket\Server( 8080);
399
+ $second = new React\Socket\SocketServer('127.0.0.1: 8080' );
398
400
```
399
401
400
402
> Note that these error conditions may vary depending on your system and/or
401
403
configuration.
402
404
See the exception message and code for more details about the actual error
403
405
condition.
404
406
405
- This class takes an optional ` LoopInterface|null $loop ` parameter that can be used to
406
- pass the event loop instance to use for this object. You can use a ` null ` value
407
- here in order to use the [ default loop] ( https://github.com/reactphp/event-loop#loop ) .
408
- This value SHOULD NOT be given unless you're sure you want to explicitly use a
409
- given event loop instance.
410
-
411
407
Optionally, you can specify [ TCP socket context options] ( https://www.php.net/manual/en/context.socket.php )
412
408
for the underlying stream socket resource like this:
413
409
414
410
``` php
415
- $server = new React\Socket\Server ('[::1]:8080', null , array(
411
+ $socket = new React\Socket\SocketServer ('[::1]:8080', array(
416
412
'tcp' => array(
417
413
'backlog' => 200,
418
414
'so_reuseport' => true,
@@ -426,8 +422,6 @@ $server = new React\Socket\Server('[::1]:8080', null, array(
426
422
and/or PHP version.
427
423
Passing unknown context options has no effect.
428
424
The ` backlog ` context option defaults to ` 511 ` unless given explicitly.
429
- For BC reasons, you can also pass the TCP socket context options as a simple
430
- array without wrapping this in another array under the ` tcp ` key.
431
425
432
426
You can start a secure TLS (formerly known as SSL) server by simply prepending
433
427
the ` tls:// ` URI scheme.
@@ -438,7 +432,7 @@ which in its most basic form may look something like this if you're using a
438
432
PEM encoded certificate file:
439
433
440
434
``` php
441
- $server = new React\Socket\Server ('tls://127.0.0.1:8080', null , array(
435
+ $socket = new React\Socket\SocketServer ('tls://127.0.0.1:8080', array(
442
436
'tls' => array(
443
437
'local_cert' => 'server.pem'
444
438
)
@@ -454,7 +448,7 @@ If your private key is encrypted with a passphrase, you have to specify it
454
448
like this:
455
449
456
450
``` php
457
- $server = new React\Socket\Server ('tls://127.0.0.1:8000', null , array(
451
+ $socket = new React\Socket\SocketServer ('tls://127.0.0.1:8000', array(
458
452
'tls' => array(
459
453
'local_cert' => 'server.pem',
460
454
'passphrase' => 'secret'
@@ -467,7 +461,7 @@ SSLv2/SSLv3. As of PHP 5.6+ you can also explicitly choose the TLS version you
467
461
want to negotiate with the remote side:
468
462
469
463
``` php
470
- $server = new React\Socket\Server ('tls://127.0.0.1:8000', null , array(
464
+ $socket = new React\Socket\SocketServer ('tls://127.0.0.1:8000', array(
471
465
'tls' => array(
472
466
'local_cert' => 'server.pem',
473
467
'crypto_method' => STREAM_CRYPTO_METHOD_TLSv1_2_SERVER
@@ -488,7 +482,7 @@ Whenever a client connects, it will emit a `connection` event with a connection
488
482
instance implementing [ ` ConnectionInterface ` ] ( #connectioninterface ) :
489
483
490
484
``` php
491
- $server ->on('connection', function (React\Socket\ConnectionInterface $connection) {
485
+ $socket ->on('connection', function (React\Socket\ConnectionInterface $connection) {
492
486
echo 'Plaintext connection from ' . $connection->getRemoteAddress() . PHP_EOL;
493
487
494
488
$connection->write('hello there!' . PHP_EOL);
@@ -498,10 +492,20 @@ $server->on('connection', function (React\Socket\ConnectionInterface $connection
498
492
499
493
See also the [ ` ServerInterface ` ] ( #serverinterface ) for more details.
500
494
501
- > Note that the ` Server ` class is a concrete implementation for TCP/IP sockets.
495
+ This class takes an optional ` LoopInterface|null $loop ` parameter that can be used to
496
+ pass the event loop instance to use for this object. You can use a ` null ` value
497
+ here in order to use the [ default loop] ( https://github.com/reactphp/event-loop#loop ) .
498
+ This value SHOULD NOT be given unless you're sure you want to explicitly use a
499
+ given event loop instance.
500
+
501
+ > Note that the ` SocketServer ` class is a concrete implementation for TCP/IP sockets.
502
502
If you want to typehint in your higher-level protocol implementation, you SHOULD
503
503
use the generic [ ` ServerInterface ` ] ( #serverinterface ) instead.
504
504
505
+ > Changelog v1.9.0: This class has been added with an improved constructor signature
506
+ as a replacement for the previous ` Server ` class in order to avoid any ambiguities.
507
+ The previous name has been deprecated and should not be used anymore.
508
+
505
509
### Advanced server usage
506
510
507
511
#### TcpServer
0 commit comments