Skip to content

Instantly share code, notes, and snippets.

@IvanChepurnyi
Created September 8, 2016 14:38
Show Gist options
  • Select an option

  • Save IvanChepurnyi/1b28b77c019674becca3f96a6fa66983 to your computer and use it in GitHub Desktop.

Select an option

Save IvanChepurnyi/1b28b77c019674becca3f96a6fa66983 to your computer and use it in GitHub Desktop.

Revisions

  1. IvanChepurnyi created this gist Sep 8, 2016.
    43 changes: 43 additions & 0 deletions test.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    <?php

    require 'vendor/autoload.php';

    $counter = new stdClass();
    $counter->counter = 100000;
    $counter->requests = 0;

    $app = function ($request, $response) use ($counter) {
    if ($request->getPath() === '/favicon.ico') {
    $response->writeHead(404, array('Content-Type' => 'text/plain'));
    } else {
    $response->writeHead(200, array('Content-Type' => 'text/plain'));
    }

    $counter->requests ++;
    switch ($request->getPath()) {
    case '/dec':
    $counter->counter --;
    $response->end('Stock decreased');
    break;
    case '/info':
    $response->write('Memory usage: ' . round(memory_get_usage()/(1024*1024), 4). "M\n");
    $response->write('Requests processed: ' . $counter->requests . "\n");
    $response->write('Current counter value: ' . $counter->counter . "\n");
    $response->end();
    break;
    default:
    $response->end('Current counter value: ' . $counter->counter . "\n");
    break;
    }
    };

    $loop = React\EventLoop\Factory::create();
    $socket = new React\Socket\Server($loop);
    $http = new React\Http\Server($socket, $loop);

    $http->on('request', $app);
    echo "Server running at http://127.0.0.1:1337\n";

    $socket->listen(1337);
    $loop->run();