PHP Classes

File: vendor/react/event-loop/examples/21-http-server.php

Recommend this page to a friend!
  Classes of Stanley Aloh   Tic Tac Toe Multiplayer   vendor/react/event-loop/examples/21-http-server.php   Download  
File: vendor/react/event-loop/examples/21-http-server.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Tic Tac Toe Multiplayer
Manage the interactions ofTic-Tac-Toe game players
Author: By
Last change:
Date: 9 days ago
Size: 1,060 bytes
 

Contents

Class file image Download
<?php

require __DIR__ . '/../vendor/autoload.php';

$loop = React\EventLoop\Factory::create();

// start TCP/IP server on localhost:8080
// for illustration purposes only, should use react/socket instead
$server = stream_socket_server('tcp://127.0.0.1:8080');
if (!
$server) {
    exit(
1);
}
stream_set_blocking($server, false);

// wait for incoming connections on server socket
$loop->addReadStream($server, function ($server) use ($loop) {
   
$conn = stream_socket_accept($server);
   
$data = "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nHi\n";
   
$loop->addWriteStream($conn, function ($conn) use (&$data, $loop) {
       
$written = fwrite($conn, $data);
        if (
$written === strlen($data)) {
           
fclose($conn);
           
$loop->removeWriteStream($conn);
        } else {
           
$data = substr($data, $written);
        }
    });
});

$loop->addPeriodicTimer(5, function () {
   
$memory = memory_get_usage() / 1024;
   
$formatted = number_format($memory, 3).'K';
    echo
"Current memory usage: {$formatted}\n";
});

$loop->run();