orolyn / orolyn
Orolyn PHP Library
dev-master
2023-02-11 18:17 UTC
Requires
- php-64bit: ^8.1
- ext-ds: *
- ext-eio: *
- ext-ffi: *
- ext-mbstring: *
- ext-pcntl: *
- ext-sockets: *
- ext-zlib: *
- nikic/php-parser: ^4.14
- psr/http-message: ^1.0
- symfony/console: ^6.0
- symfony/process: ^6.0
Requires (Dev)
- doctrine/rst-parser: 0.5.2
- phpunit/phpunit: ^9.6
- vimeo/psalm: ^4.15
This package is auto-updated.
Last update: 2024-04-11 21:12:31 UTC
README
A fiber based asynchronous tooling library for server development.
Documentation
Documentation is provided at readthedocs.org
What is it
This library provides general purpose tools for server development. The primary goal of this library is to enable development with a familiar synchronous pattern while utilizing fibers to allow for multithreading-like operations.
use Orolyn\Concurrency\Application; use Orolyn\Concurrency\TaskScheduler; use Orolyn\Net\Http\FailedHttpRequestException; use Orolyn\Net\Http\HttpRequestContext; use Orolyn\Net\Http\HttpServer; use Orolyn\Net\Http\WebSocket\InvalidWebSocketContextException; use Orolyn\Net\Http\WebSocket\WebSocket; use Orolyn\Net\Http\WebSocket\WebSocketClosedException; use Orolyn\Net\IPAddress; use Orolyn\Net\IPEndPoint; use function Orolyn\Lang\Async; class ApplicationServer extends Application { public function main(): void { $httpServer = new HttpServer(); $httpServer->listen(new IPEndPoint(IPAddress::parse('0.0.0.0'), 9999)); while ($httpServer->isListening()) { try { $context = $httpServer->accept(); Async(fn() => $this->handleRequest($context)); } catch (FailedHttpRequestException $exception) { /* Log error */ } } } private function handleRequest(HttpRequestContext $context): void { try { $websocket = WebSocket::create($context); } catch (InvalidWebSocketContextException $exception) { /* Log error */ return; } try { $websocket->send('Hi!!!!!'); $data = $websocket->receive()->getData(); if ('okthxbye' === $data) { $websocket->send('Oh..'); $websocket->close(); return; } /* Lets get to work */ } catch (WebSocketClosedException $exception) { return; } } } TaskScheduler::run(new ApplicationServer());