Code Monkey home page Code Monkey logo

Orolyn PHP Library

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());

orolyn's Projects

orolyn doesnโ€™t have any public repositories yet.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.