PHP code example of upscale / swoole-reflection

1. Go to this page and download the library: Download upscale/swoole-reflection library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

upscale / swoole-reflection example snippets


$server = new \Swoole\Http\Server('127.0.0.1', 8080);
$server->on('request', function ($request, $response) {
   $response->end("Served by Swoole server\n");
});

$reflection = new \Upscale\Swoole\Reflection\Http\Server($server);
$middleware = $reflection->getMiddleware();
$reflection->setMiddleware(function ($request, $response) use ($middleware) {
   $response->header('Content-Type', 'text/plain');
   $middleware($request, $response);
});

$server->start();

$server->on('request', function ($request, $response) {
    $callback = function () use ($request, $response) {
        $response->header('Content-Type', 'text/plain');
    };
    $response = new \Upscale\Swoole\Reflection\Http\Response\Observable($response);
    $response->onHeadersSentBefore($callback);    
    $response->end("Served by Swoole server\n");
});

$server->on('request', function ($request, $response) use ($server) {
    $response = new \Upscale\Swoole\Reflection\Http\Response\Observable($response);
    $response->onBodyAppend(function (&$content) use ($server) {
        $content .= "<!-- Served by worker {$server->worker_id} -->\n";
    });
    $response->header('Content-Type', 'text/html');
    $response->end("Served by <b>Swoole server</b>\n");
});