Exemplo n.º 1
0
 public function run(int $port, string $address = '*')
 {
     $server = new Server($this);
     $server->listen($port, $address);
     \Icicle\Loop\run();
 }
Exemplo n.º 2
0
<?php

require "vendor/autoload.php";
use Icicle\Http\Message\RequestInterface;
use Icicle\Http\Server\Server;
use Icicle\Loop;
use Icicle\Socket\SocketInterface;
$dispatcher = FastRoute\simpleDispatcher(function (FastRoute\RouteCollector $collector) {
    require __DIR__ . "/routes.php";
});
$server = new Server(function (RequestInterface $request, SocketInterface $socket) use($dispatcher) {
    $dispatched = $dispatcher->dispatch($request->getMethod(), $request->getRequestTarget());
    switch ($dispatched[0]) {
        case FastRoute\Dispatcher::NOT_FOUND:
            // there is no matching route
            break;
        case FastRoute\Dispatcher::METHOD_NOT_ALLOWED:
            // the method called on this route is invalid
            break;
        case FastRoute\Dispatcher::FOUND:
            (yield call_user_func($dispatched[1], $request, $socket, $dispatched[2]));
            break;
    }
});
$server->listen(8000);
Loop\run();
Exemplo n.º 3
0
use Icicle\Http\Message\Response;
use Icicle\Http\Server\Server;
use Icicle\Loop;
use Icicle\Socket\Client\ClientInterface;
use League\Route\Http\Exception\MethodNotAllowedException;
use League\Route\Http\Exception\NotFoundException;
use League\Route\RouteCollection;
use League\Route\Strategy\UriStrategy;
$server = new Server(function (RequestInterface $request, ClientInterface $client) {
    $router = new RouteCollection();
    $router->setStrategy(new UriStrategy());
    require __DIR__ . "/routes.php";
    $dispatcher = $router->getDispatcher();
    try {
        $result = $dispatcher->dispatch($request->getMethod(), $request->getRequestTarget());
        $status = 200;
        $content = $result->getContent();
    } catch (NotFoundException $exception) {
        $status = 404;
        $content = "not found";
    } catch (MethodNotAllowedException $exception) {
        $status = 405;
        $content = "method not allowed";
    }
    $response = new Response($status);
    $response = $response->withHeader("Content-Type", "text/html");
    (yield $response->getBody()->end($content));
    (yield $response);
});
$server->listen(9001);
Loop\run();