PHP Classes

File: server-http.php

Recommend this page to a friend!
  Classes of Ahmad Mustapha   ReactPHP Chat Client   server-http.php   Download  
File: server-http.php
Role: Example script
Content type: text/plain
Description: Example script
Class: ReactPHP Chat Client
Implement a live chat system based on Web Sockets
Author: By
Last change:
Date: 3 years ago
Size: 1,310 bytes
 

Contents

Class file image Download
<?php
error_reporting
(E_ALL);

use
App\Http\Server;
use
App\Core\Router\Route;
use
React\EventLoop\Factory;
use
React\Http\Server as HttpServer;
use
React\Socket\Server as SocketServer;
use
React\Filesystem\Filesystem;

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

$loop = Factory::create();
$filesystem = Filesystem::create($loop);

$serverConfig = [
   
'host' => '127.0.0.1',
   
'port' => 10001
];
$routes = Route::getRoutes();

$httpServer = new HttpServer($loop, new Server($routes));

//Handle all errors
$httpServer->on('error', function ($exception){
   
$file = __DIR__ . '/storage/logs/' . time() . '.log';
   
$log = json_encode([
       
'message' => $exception->getMessage(),
       
'file' => $exception->getFile(),
       
'line' => $exception->getLine(),
       
'code' => $exception->getCode(),
    ],
JSON_PRETTY_PRINT);
   
file_put_contents($file, $log);
    echo
"\n[*] Error: {$exception->getMessage()} => {$exception->getFile()} @ Line {$exception->getLine()}\n";
});

setLoop($loop);

require
'app/Core/Helpers/httpHelperFunctions.php';
require
'app/Core/event-listeners.php';

echo
"\n[*] Http-Server running on port {$serverConfig['port']}\n";

$socket = new SocketServer("{$serverConfig['host']}:{$serverConfig['port']}", $loop);
$httpServer->listen($socket);
$loop->run();