예제 #1
0
$connectionHandler = new ConnectionHandler($router);
$fileHandler = new StaticFile($documentRoot, $loop);
$router->setFileHandler($fileHandler);
$connectionHandler->on('close', function ($conn) use(&$connections) {
    foreach ($connections as $key => $data) {
        $connection = $data['conn'];
        if ($connection === $conn) {
            unset($connections[$key]);
            break;
        }
    }
});
$loop->addPeriodicTimer(1, function ($timer) use(&$connections) {
    foreach ($connections as $data) {
        echo get_class($data['conn']) . PHP_EOL;
        $connection = $data['conn'];
        $data['msg']->setMessage(array('id' => $data['startedAt'], 'msg' => time()));
        $connection->send($data['msg']);
    }
});
$router->addRoute('/sse.php', function ($request, $response, $connection) use(&$connections) {
    $response->setHeaders(['Content-Type' => 'text/event-stream', 'Cache-Control' => 'no-cache']);
    $startedAt = time();
    $msg = new EventMessage($startedAt);
    $msg->setMessage(array('id' => $startedAt, 'msg' => time()));
    $response->setBody($msg);
    $connection->send($response);
    $connections[] = ['conn' => $connection, 'msg' => $msg, 'id' => $startedAt];
});
$httpSocket = new React\Socket\Server($loop);
$httpSocket->listen('81', '0.0.0.0');
$httpServer = new IoServer(new HttpServer($connectionHandler), $httpSocket, $loop);
<?php

require __DIR__ . '/../vendor/autoload.php';
/** @var $loop \React\EventLoop\ExtEventLoop */
$cfg = new \EventConfig();
$cfg->requireFeatures(\EventConfig::FEATURE_FDS);
$loop = new \React\EventLoop\ExtEventLoop($cfg);
$dir = getcwd();
$fd = fopen($dir . DIRECTORY_SEPARATOR . 'test.dmg', "r");
stream_set_blocking($fd, 0);
$stream = new \React\Stream\Stream($fd, $loop);
$stream->on('data', function () {
    echo 'test' . PHP_EOL;
});
$loop->addPeriodicTimer(0.01, function () {
    echo "Timer" . PHP_EOL;
});
$loop->run();