Overview

Namespaces

  • Evenement
  • None
  • PHP
  • Psr
    • Http
      • Message
  • Ratchet
    • Http
    • RFC6455
      • Handshake
      • Messaging
    • Server
    • Session
      • Serialize
      • Storage
        • Proxy
    • Wamp
    • WebSocket
  • React
    • EventLoop
      • Tick
      • Timer
    • Socket
    • Stream
  • Symfony
    • Component
      • HttpFoundation
        • Session
          • Attribute
          • Flash
          • Storage
            • Handler
            • Proxy
      • Routing
        • Annotation
        • Exception
        • Generator
          • Dumper
        • Loader
          • DependencyInjection
        • Matcher
          • Dumper
        • Tests
          • Annotation
          • Fixtures
            • AnnotatedClasses
            • OtherAnnotatedClasses
          • Generator
            • Dumper
          • Loader
          • Matcher
            • Dumper

Classes

  • Connector
  • DnsConnector
  • LimitingServer
  • SecureConnector
  • SecureServer
  • Server
  • TcpConnector
  • TimeoutConnector
  • UnixConnector

Interfaces

  • ConnectionInterface
  • ConnectorInterface
  • ServerInterface
  • Overview
  • Namespace
  • Class
  • Tree

Class Server

The Server class implements the ServerInterface and is responsible for accepting plaintext TCP/IP connections.

`php $server = new Server(8080, $loop); `

Whenever a client connects, it will emit a connection event with a connection instance implementing ConnectionInterface:

`php $server->on('connection', function (ConnectionInterface $connection) { echo 'Plaintext connection from ' . $connection->getRemoteAddress() . PHP_EOL; $connection->write('hello there!' . PHP_EOL); … }); `

See also the ServerInterface for more details.

Note that the Server class is a concrete implementation for TCP/IP sockets. If you want to typehint in your higher-level protocol implementation, you SHOULD use the generic ServerInterface instead.

Evenement\EventEmitter implements Evenement\EventEmitterInterface uses Evenement\EventEmitterTrait
Extended by React\Socket\Server implements React\Socket\ServerInterface
Final
Namespace: React\Socket
See: React\Socket\ServerInterface
See: React\Socket\ConnectionInterface
Located at Server.php
Methods summary
public
# __construct( string|integer $uri, React\EventLoop\LoopInterface $loop, array $context = array() )

Creates a plaintext TCP/IP socket server and starts listening on the given address

Creates a plaintext TCP/IP socket server and starts listening on the given address

This starts accepting new incoming connections on the given address. See also the connection event documented in the ServerInterface for more details.

`php $server = new Server(8080, $loop); `

As above, the $uri parameter can consist of only a port, in which case the server will default to listening on the localhost address 127.0.0.1, which means it will not be reachable from outside of this system.

In order to use a random port assignment, you can use the port 0:

`php $server = new Server(0, $loop); $address = $server->getAddress(); `

In order to change the host the socket is listening on, you can provide an IP address through the first parameter provided to the constructor, optionally preceded by the tcp:// scheme:

`php $server = new Server('192.168.0.1:8080', $loop); `

If you want to listen on an IPv6 address, you MUST enclose the host in square brackets:

`php $server = new Server('[::1]:8080', $loop); `

If the given URI is invalid, does not contain a port, any other scheme or if it contains a hostname, it will throw an InvalidArgumentException:

`php // throws InvalidArgumentException due to missing port $server = new Server('127.0.0.1', $loop); `

If the given URI appears to be valid, but listening on it fails (such as if port is already in use or port below 1024 may require root access etc.), it will throw a RuntimeException:

`php $first = new Server(8080, $loop);

// throws RuntimeException because port is already in use $second = new Server(8080, $loop); `

Note that these error conditions may vary depending on your system and/or configuration. See the exception message and code for more details about the actual error condition.

Optionally, you can specify [socket context options](http://php.net/manual/en/context.socket.php) for the underlying stream socket resource like this:

`php $server = new Server('[::1]:8080', $loop, array( 'backlog' => 200, 'so_reuseport' => true, 'ipv6_v6only' => true )); `

Note that available [socket context options](http://php.net/manual/en/context.socket.php), their defaults and effects of changing these may vary depending on your system and/or PHP version. Passing unknown context options has no effect.

Parameters

$uri
string|integer
$uri
$loop
React\EventLoop\LoopInterface
$loop
$context
array
$context

Throws

InvalidArgumentException
if the listening address is invalid
RuntimeException
if listening on this address fails (already in use etc.)
public ?string
# getAddress( )

Returns the full address (IP and port) this server is currently listening on

Returns the full address (IP and port) this server is currently listening on

`php $address = $server->getAddress(); echo 'Server listening on ' . $address . PHP_EOL; `

It will return the full address (IP and port) or NULL if it is unknown (not applicable to this server socket or already closed).

If this is a TCP/IP based server and you only want the local port, you may use something like this:

`php $address = $server->getAddress(); $port = parse_url('tcp://' . $address, PHP_URL_PORT); echo 'Server listening on port ' . $port . PHP_EOL; `

Returns

?string
the full listening address (IP and port) or NULL if it is unknown (not applicable to this server socket or already closed)

Implementation of

React\Socket\ServerInterface::getAddress()
public
# pause( )

Pauses accepting new incoming connections.

Pauses accepting new incoming connections.

Removes the socket resource from the EventLoop and thus stop accepting new connections. Note that the listening socket stays active and is not closed.

This means that new incoming connections will stay pending in the operating system backlog until its configurable backlog is filled. Once the backlog is filled, the operating system may reject further incoming connections until the backlog is drained again by resuming to accept new connections.

Once the server is paused, no futher connection events SHOULD be emitted.

`php $server->pause();

$server->on('connection', assertShouldNeverCalled()); `

This method is advisory-only, though generally not recommended, the server MAY continue emitting connection events.

Unless otherwise noted, a successfully opened server SHOULD NOT start in paused state.

You can continue processing events by calling resume() again.

Note that both methods can be called any number of times, in particular calling pause() more than once SHOULD NOT have any effect. Similarly, calling this after close() is a NO-OP.

See

React\Socket\Server::resume()

Implementation of

React\Socket\ServerInterface::pause()
public
# resume( )

Resumes accepting new incoming connections.

Resumes accepting new incoming connections.

Re-attach the socket resource to the EventLoop after a previous pause().

`php $server->pause();

$loop->addTimer(1.0, function () use ($server) { $server->resume(); }); `

Note that both methods can be called any number of times, in particular calling resume() without a prior pause() SHOULD NOT have any effect. Similarly, calling this after close() is a NO-OP.

See

React\Socket\Server::pause()

Implementation of

React\Socket\ServerInterface::resume()
public
# close( )

Shuts down this listening socket

Shuts down this listening socket

This will stop listening for new incoming connections on this socket.

Calling this method more than once on the same instance is a NO-OP.

Implementation of

React\Socket\ServerInterface::close()
Methods inherited from Evenement\EventEmitterInterface
emit(), listeners(), on(), once(), removeAllListeners(), removeListener()
Methods used from Evenement\EventEmitterTrait
(), (), (), (), (), ()
Properties used from Evenement\EventEmitterTrait
$listeners
Ratchet API documentation generated by ApiGen 2.8.0