Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
trowski committed Feb 22, 2020
1 parent 020a3de commit 84b4923
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
36 changes: 18 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ are always welcome!

## Requirements

- PHP 7.1+
- PHP 7.2+

## Example

Expand All @@ -32,16 +32,16 @@ are always welcome!
// amphp/http-server-static-content
// amphp/log

use Amp\Http\Server\HttpServer;
use Amp\Http\Server\Request;
use Amp\Http\Server\Response;
use Amp\Http\Server\Router;
use Amp\Http\Server\Server;
use Amp\Http\Server\StaticContent\DocumentRoot;
use Amp\Log\ConsoleFormatter;
use Amp\Log\StreamHandler;
use Amp\Loop;
use Amp\Promise;
use Amp\Socket;
use Amp\Socket\Server as SocketServer;
use Amp\Success;
use Amp\Websocket\Client;
use Amp\Websocket\Message;
Expand All @@ -64,7 +64,7 @@ $websocket = new class extends Websocket {

public function handleClient(Client $client, Request $request, Response $response): Promise
{
return call(function() use($client) {
return call(function() use ($client): \Generator {
while ($message = yield $client->receive()) {
\assert($message instanceof Message);
$this->broadcast(\sprintf('%d: %s', $client->getId(), yield $message->buffer()));
Expand All @@ -73,23 +73,23 @@ $websocket = new class extends Websocket {
}
};

$sockets = [
Socket\listen('127.0.0.1:1337'),
Socket\listen('[::1]:1337'),
];
Loop::run(function () use ($websocket): Promise {
$sockets = [
SocketServer::listen('127.0.0.1:1337'),
SocketServer::listen('[::1]:1337'),
];

$router = new Router;
$router->addRoute('GET', '/broadcast', $websocket);
$router->setFallback(new DocumentRoot(__DIR__ . '/public'));
$router = new Router;
$router->addRoute('GET', '/broadcast', $websocket);
$router->setFallback(new DocumentRoot(__DIR__ . '/public'));

$logHandler = new StreamHandler(getStdout());
$logHandler->setFormatter(new ConsoleFormatter);
$logger = new Logger('server');
$logger->pushHandler($logHandler);
$logHandler = new StreamHandler(getStdout());
$logHandler->setFormatter(new ConsoleFormatter);
$logger = new Logger('server');
$logger->pushHandler($logHandler);

$server = new Server($sockets, $router, $logger);
$server = new HttpServer($sockets, $router, $logger);

Loop::run(function () use ($server) {
yield $server->start();
return $server->start();
});
```
3 changes: 2 additions & 1 deletion src/ClientHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public function handleHandshake(Request $request, Response $response): Promise;
/**
* This method is called when a new websocket connection is established on the endpoint.
* The method may handle all messages itself or pass the connection along to a separate
* handler if desired.
* handler if desired. The client connection is closed when the promise returned from
* this method resolves.
*
* ```
* return Amp\call(function () use ($client) {
Expand Down
2 changes: 0 additions & 2 deletions src/Websocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,6 @@ public function getOptions(): Options
* Invoked when the server is starting.
* Server sockets have been opened, but are not yet accepting client connections. This method should be used to set
* up any necessary state for responding to requests, including starting loop watchers such as timers.
* Note: Implementations overriding this method must always call the parent method.
*
* @param Server $server
*
Expand All @@ -385,7 +384,6 @@ public function onStart(Server $server): Promise
* Invoked when the server has initiated stopping.
* No further requests are accepted and any connected clients should be closed gracefully and any loop watchers
* cancelled.
* Note: Implementations overriding this method must always call the parent method.
*
* @param Server $server
*
Expand Down

0 comments on commit 84b4923

Please sign in to comment.