示例#1
0
 public function testRequestEvent()
 {
     $io = new ServerStub();
     $i = 0;
     $server = new Server($io);
     $server->on('request', function ($conn, $request, $response) use(&$i) {
         $i++;
         $this->assertInstanceOf('ExpressiveAsync\\Test\\ConnectionStub', $conn);
         $this->assertInstanceOf('ExpressiveAsync\\ServerRequest', $request);
         $this->assertSame('/', $request->getUri()->getPath());
         $this->assertSame('GET', $request->getMethod());
         $this->assertSame('127.0.0.1', $request->getRemoteAddress());
         $this->assertInstanceOf('ExpressiveAsync\\Response', $response);
     });
     $conn = new ConnectionStub();
     $io->emit('connection', array($conn));
     $data = $this->createGetRequest();
     $conn->emit('data', array($data));
     $this->assertSame(1, $i);
 }
示例#2
0
<?php

require dirname(__DIR__) . '/vendor/autoload.php';
use Zend\Diactoros;
use ExpressiveAsync\Server;
use React\EventLoop\Factory;
use React\Socket\Server as SocketServer;
use ExpressiveAsync\Application;
use React\Promise\Deferred;
$serviceManager = new \Zend\ServiceManager\ServiceManager();
$eventLoop = Factory::create();
$socketServer = new SocketServer($eventLoop);
$httpServer = new Server($socketServer);
$serviceManager->setFactory('EventLoop', function () use($eventLoop) {
    return $eventLoop;
});
$serviceManager->setInvokableClass('Zend\\Expressive\\Router\\RouterInterface', 'Zend\\Expressive\\Router\\FastRouteRouter');
$router = new \Zend\Expressive\Router\FastRouteRouter();
$router->addRoute(new \Zend\Expressive\Router\Route('/', function ($request, $response) use($eventLoop) {
    echo 'Home executed' . PHP_EOL;
    return new Diactoros\Response\HtmlResponse('Hello World.');
}, ['GET'], 'home'));
$router->addRoute(new \Zend\Expressive\Router\Route('/deferred', function ($request, $response) use($eventLoop) {
    // create a request, wait 1-5 seconds and then return a response.
    $deferred = new Deferred();
    $eventLoop->addTimer(rand(1, 5), function () use($deferred) {
        echo 'Timer executed' . PHP_EOL;
        $deferred->resolve(new Diactoros\Response\HtmlResponse('Deferred response.'));
    });
    return new \ExpressiveAsync\DeferredResponse($deferred->promise());
}, ['GET'], 'deferred'));
示例#3
0
<?php

require dirname(__DIR__) . '/vendor/autoload.php';
use Zend\Diactoros;
use ExpressiveAsync\Server;
use React\EventLoop\Factory;
use React\Socket\Server as SocketServer;
use ExpressiveAsync\Application;
use React\Promise\Deferred;
$serviceManager = new \Zend\ServiceManager\ServiceManager();
$eventLoop = Factory::create();
$socketServer = new SocketServer($eventLoop);
$httpServer = new Server($socketServer);
$serviceManager->setFactory('EventLoop', function () use($eventLoop) {
    return $eventLoop;
});
$serviceManager->setInvokableClass('Zend\\Expressive\\Router\\RouterInterface', 'Zend\\Expressive\\Router\\FastRouteRouter');
$router = new \Zend\Expressive\Router\FastRouteRouter();
$router->addRoute(new \Zend\Expressive\Router\Route('/', function ($request, $response) use($eventLoop) {
    echo 'Home executed' . PHP_EOL;
    return new Diactoros\Response\HtmlResponse('Hello World.');
}, ['GET'], 'home'));
$router->addRoute(new \Zend\Expressive\Router\Route('/deferred', function ($request, $response) use($eventLoop) {
    // create a request, wait 1-5 seconds and then return a response.
    $deferred = new Deferred();
    $eventLoop->addTimer(rand(1, 5), function () use($deferred) {
        echo 'Timer executed' . PHP_EOL;
        $deferred->resolve(new Diactoros\Response\HtmlResponse('Deferred response.'));
    });
    return new \ExpressiveAsync\DeferredResponse($deferred->promise());
}, ['GET'], 'deferred'));